blob: 12c999febb57bd0da85d16e30fe51625bc66d350 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
class PostInteractionPresenter
def initialize(post, current_user)
@post = post
@current_user = current_user
end
def as_json(_options={})
{
likes: as_api(@post.likes),
reshares: PostPresenter.as_collection(@post.reshares, :as_json, @current_user),
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
private
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
|