mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-09 05:15:53 +10:00
#16 Add last sharer and sharers count to show
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
class EachPostSerializer < ActiveModel::Serializer
|
||||
|
||||
attributes :id, :viewed_times, :url, :title, :type, :processed, :good_format, :created_at, :likes_count, :comments_count,
|
||||
:summary, :thumbnail, :first_sharer
|
||||
:summary, :thumbnail, :first_sharer, :last_sharer, :sharers_count
|
||||
|
||||
def thumbnail
|
||||
object.thumbnail.try(:file).try(:url)
|
||||
@@ -9,6 +9,22 @@ class EachPostSerializer < ActiveModel::Serializer
|
||||
|
||||
def first_sharer
|
||||
user = ShareInstance.where(sharable_id: object.id).first.user
|
||||
user_info_json(user)
|
||||
end
|
||||
|
||||
def last_sharer
|
||||
return nil if ShareInstance.where(sharable_id: object.id).count == 1
|
||||
user = ShareInstance.where(sharable_id: object.id).last.user
|
||||
user_info_json(user)
|
||||
end
|
||||
|
||||
def sharers_count
|
||||
ShareInstance.where(sharable_id: object.id).count
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def user_info_json(user)
|
||||
{
|
||||
id: user.try(:id),
|
||||
name: user.try(:name),
|
||||
|
||||
Reference in New Issue
Block a user