From 8681de91f4458e08faaa4fe96a2aff81bf5458d2 Mon Sep 17 00:00:00 2001 From: junv Date: Mon, 12 Oct 2015 00:06:12 +0800 Subject: [PATCH] #16 Add last sharer and sharers count to show --- app/serializers/each_post_serializer.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/serializers/each_post_serializer.rb b/app/serializers/each_post_serializer.rb index 1a46d5c..eecdd33 100644 --- a/app/serializers/each_post_serializer.rb +++ b/app/serializers/each_post_serializer.rb @@ -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),