socialforge/app/api/mobile/entities/blog_comment.rb

66 lines
2.0 KiB
Ruby
Raw Normal View History

2016-03-30 15:51:27 +08:00
module Mobile
module Entities
class BlogComment < Grape::Entity
include ApplicationHelper
include ApiHelper
def self.blog_comment_expose(f)
expose f do |u,opt|
if u.is_a?(Hash) && u.key?(f)
u[f]
elsif u.is_a?(::BlogComment)
if u.respond_to?(f)
if f == :created_at
format_time( u.send(f))
else
u.send(f)
end
else
2016-03-30 17:49:05 +08:00
case f
2016-04-07 16:40:21 +08:00
when :praise_count
2016-03-30 17:49:05 +08:00
get_activity_praise_num(u)
when :lasted_comment
time_from_now(u.created_at)
2016-04-07 16:40:21 +08:00
when :act_type
'BlogComment'
when :act_id
u.id
2016-04-08 16:58:27 +08:00
when :comment_count
u.children.count
2016-03-30 17:49:05 +08:00
end
2016-03-30 15:51:27 +08:00
end
end
end
end
expose :user, using: Mobile::Entities::User do |c, opt|
if c.is_a?(::BlogComment)
c.author
end
end
2016-04-07 16:40:21 +08:00
blog_comment_expose :act_type
blog_comment_expose :act_id
2016-03-30 15:51:27 +08:00
blog_comment_expose :blog_id
blog_comment_expose :title
blog_comment_expose :content
2016-04-08 16:58:27 +08:00
blog_comment_expose :comment_count
2016-03-30 15:51:27 +08:00
blog_comment_expose :created_at
2016-03-30 17:49:05 +08:00
blog_comment_expose :lasted_comment
2016-03-30 15:51:27 +08:00
blog_comment_expose :id
2016-05-13 15:01:36 +08:00
blog_comment_expose :locked
2016-04-07 16:40:21 +08:00
blog_comment_expose :praise_count
2016-03-30 15:51:27 +08:00
expose :blog_comment_children, using:Mobile::Entities::BlogComment do |c,opt|
if c.is_a? (::BlogComment)
2016-04-07 15:09:17 +08:00
c.children.reverse
2016-03-30 15:51:27 +08:00
end
end
2016-04-07 16:40:21 +08:00
expose :has_praise, if: lambda { |instance, options| options[:user] } do |instance, options|
2016-04-07 11:16:38 +08:00
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
2016-03-30 15:51:27 +08:00
end
end
end