2015-01-20 15:17:02 +08:00
|
|
|
module Mobile
|
|
|
|
module Entities
|
|
|
|
class News < Grape::Entity
|
2015-02-10 10:29:47 +08:00
|
|
|
include Redmine::I18n
|
2015-01-20 15:17:02 +08:00
|
|
|
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)
|
2015-02-10 10:29:47 +08:00
|
|
|
if field == :created_on
|
|
|
|
format_time(n.send(field)) if n.respond_to?(field)
|
|
|
|
else
|
|
|
|
n.send(field) if n.respond_to?(field)
|
|
|
|
end
|
|
|
|
|
2015-01-20 17:42:18 +08:00
|
|
|
end
|
|
|
|
|
2015-01-20 15:17:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-02-10 10:29:47 +08:00
|
|
|
news_expose :id
|
2015-01-20 15:17:02 +08:00
|
|
|
#新闻标题
|
|
|
|
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
|
2015-01-20 15:17:02 +08:00
|
|
|
#作者id
|
|
|
|
news_expose :author_id
|
|
|
|
#作者名
|
|
|
|
news_expose :author_name
|
|
|
|
#新闻内容
|
2015-01-20 17:42:18 +08:00
|
|
|
news_expose :description
|
2015-01-20 15:17:02 +08:00
|
|
|
#发布时间
|
2015-01-20 17:42:18 +08:00
|
|
|
news_expose :created_on
|
2015-01-20 15:17:02 +08:00
|
|
|
#评论数量
|
|
|
|
news_expose :comments_count
|
2015-01-20 17:42:18 +08:00
|
|
|
#评论
|
2015-02-04 11:57:37 +08:00
|
|
|
expose :comments, using: Mobile::Entities::Comment do |f, opt|
|
|
|
|
if f.is_a?(Hash) && f.key?(:comments)
|
|
|
|
f[:comments]
|
|
|
|
end
|
|
|
|
end
|
2015-01-20 17:42:18 +08:00
|
|
|
|
2015-01-20 15:17:02 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|