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

42 lines
973 B
Ruby
Raw Normal View History

module Mobile
module Entities
class News < Grape::Entity
def self.news_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
2015-01-20 17:42:18 +08:00
elsif f.is_a?(Hash) && !f.key?(field)
n = f[:news]
comments = f[:comments]
if n.is_a?(::News)
n.send(field) if n.respond_to?(field)
end
end
end
end
#新闻标题
news_expose :title
2015-01-20 17:42:18 +08:00
expose :author,using: Mobile::Entities::User do |f, opt|
n = f[:news]
n.author if n.respond_to?(:author)
end
#作者id
news_expose :author_id
#作者名
news_expose :author_name
#新闻内容
2015-01-20 17:42:18 +08:00
news_expose :description
#发布时间
2015-01-20 17:42:18 +08:00
news_expose :created_on
#评论数量
news_expose :comments_count
2015-01-20 17:42:18 +08:00
#评论
news_expose :comments
end
end
end