post_interaction_presenter.rb 872 Bytes
Newer Older
1 2 3 4 5 6 7 8 9
class PostInteractionPresenter
  def initialize(post, current_user)
    @post = post
    @current_user = current_user
  end

  def as_json(_options={})
    {
      likes:          as_api(@post.likes),
10
      reshares:       PostPresenter.as_collection(@post.reshares, :as_json, @current_user),
11 12 13 14 15 16 17 18
      comments:       CommentPresenter.as_collection(@post.comments.order("created_at ASC")),
      participations: as_api(participations),
      comments_count: @post.comments_count,
      likes_count:    @post.likes_count,
      reshares_count: @post.reshares_count
    }
  end

19 20
  private

21 22 23 24 25 26 27 28 29 30 31 32
  def participations
    return @post.participations.none unless @current_user
    @post.participations.where(author: @current_user.person)
  end

  def as_api(collection)
    collection.includes(author: :profile).map {|element|
      element.as_api_response(:backbone)
    }
  end
end