Commit 2a3dde1a authored by theworldbright's avatar theworldbright Committed by Jonne Haß
Browse files

Refactor PostService and extract its tests

Squashed commits:

[ada0f09] Remove favorites from Posts table

closes #6208
parent d7243971
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

## Refactor
* Drop broken correlations from the admin pages [#6223](https://github.com/diaspora/diaspora/pull/6223)
* Extract PostService from PostsController [#6208](https://github.com/diaspora/diaspora/pull/6208)

## Bug fixes
* Fix indentation and a link title on the default home page [#6212](https://github.com/diaspora/diaspora/pull/6212)
+3 −5
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ class PostsController < ApplicationController

  respond_to :html, :mobile, :json, :xml

  rescue_from Diaspora::NonPublic do |_exception|
  rescue_from Diaspora::NonPublic do
    respond_to do |format|
      format.all { render template: "errors/not_public", status: 404, layout: "application" }
    end
@@ -18,7 +18,7 @@ class PostsController < ApplicationController

  def show
    post_service = PostService.new(id: params[:id], user: current_user)
    post_service.assign_post_and_mark_notifications
    post_service.mark_user_notifications
    @post = post_service.post
    respond_to do |format|
      format.html { gon.post = post_service.present_json }
@@ -35,14 +35,12 @@ class PostsController < ApplicationController
    post_id = OEmbedPresenter.id_from_url(params.delete(:url))
    post_service = PostService.new(id: post_id, user: current_user,
                                    oembed: params.slice(:format, :maxheight, :minheight))
    post_service.assign_post
    render json: post_service.present_oembed
  end

  def interactions
    post_service = PostService.new(id: params[:id], user: current_user)
    post_service.assign_post
    respond_with(post_service.present_interactions_json)
    respond_with post_service.present_interactions_json
  end

  def destroy
+8 −6
Original line number Diff line number Diff line
@@ -149,17 +149,19 @@ class Post < ActiveRecord::Base
  end

  def self.find_public(id)
    post = Post.where(Post.key_sym(id) => id).includes(:author, comments: :author).first
    post.try(:public?) || raise(Diaspora::NonPublic)
    post || raise(ActiveRecord::RecordNotFound.new("could not find a post with id #{id}"))
    where(post_key(id) => id).includes(:author, comments: :author).first.tap do |post|
      raise ActiveRecord::RecordNotFound.new("could not find a post with id #{id}") unless post
      raise Diaspora::NonPublic unless post.public?
    end
  end

  def self.find_non_public_by_guid_or_id_with_user(id, user)
    post = user.find_visible_shareable_by_id(Post, id, key: Post.key_sym(id))
    post || raise(ActiveRecord::RecordNotFound.new("could not find a post with id #{id}"))
    user.find_visible_shareable_by_id(Post, id, key: post_key(id)).tap do |post|
      raise ActiveRecord::RecordNotFound.new("could not find a post with id #{id}") unless post
    end
  end

  def self.key_sym(id)
  def self.post_key(id)
    id.to_s.length <= 8 ? :id : :guid
  end
end
+0 −1
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ class PostPresenter
        :post_type => @post.post_type,
        :image_url => @post.image_url,
        :object_url => @post.object_url,
        :favorite => @post.favorite,
        :nsfw => @post.nsfw,
        :author => @post.author.as_api_response(:backbone),
        :o_embed_cache => @post.o_embed_cache.try(:as_api_response, :backbone),
+14 −14
Original line number Diff line number Diff line
@@ -4,7 +4,16 @@ class PostService
  def initialize(params)
    @id = params[:id]
    @user = params[:user]
    @oembed = params[:oembed]
    @oembed = params[:oembed] || {}
    assign_post
  end

  def assign_post
    if user
      @post = Post.find_non_public_by_guid_or_id_with_user(id, user)
    else
      @post = Post.find_public(id)
    end
  end

  def present_json
@@ -19,21 +28,12 @@ class PostService
    OEmbedPresenter.new(post, oembed)
  end

  def assign_post_and_mark_notifications
    assign_post
  def mark_user_notifications
    mark_corresponding_notifications_read if user
  end

  def assign_post
    if user
      @post = Post.find_non_public_by_guid_or_id_with_user(id, user)
    else
      @post = Post.find_public(id)
    end
  end

  def retract_post
    find_user_post
    raise Diaspora::NotMine unless user_owns_post?
    user.retract(@post)
  end

@@ -41,8 +41,8 @@ class PostService

  attr_reader :user, :id, :oembed

  def find_user_post
    @post = user.posts.find(id)
  def user_owns_post?
    post.author == user.person
  end

  def mark_corresponding_notifications_read
Loading