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

67 lines
1.8 KiB
Ruby

module Mobile
module Entities
class News < Grape::Entity
include Redmine::I18n
def self.news_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::News)
if field == :created_on
format_time(f.send(field)) if f.respond_to?(field)
else
f.send(field) if f.respond_to?(field)
end
elsif f.is_a?(Hash) && !f.key?(field)
n = f[:news]
comments = f[:comments]
if n.is_a?(::News)
if field == :created_on
format_time(n.send(field)) if n.respond_to?(field)
else
n.send(field) if n.respond_to?(field)
end
end
end
end
end
news_expose :id
#新闻标题
news_expose :title
expose :author,using: Mobile::Entities::User do |f, opt|
obj = nil
if f.is_a?(::News) && f.respond_to?(:author)
obj = f.send(:author)
elsif f.is_a?(Hash) && f.key?(:author)
obj = f[:author]
end
obj
end
#作者id
news_expose :author_id
#作者名
news_expose :author_name
#作者头像url
news_expose :author_img_url
#新闻内容
news_expose :description
#发布时间
news_expose :created_on
#评论数量
news_expose :comments_count
#评论
expose :comments, using: Mobile::Entities::Comment do |f, opt|
if f.is_a?(Hash) && f.key?(:comments)
f[:comments]
elsif f.is_a?(::News) && f.respond_to?(:comments)
f.send(:comments)
end
end
end
end
end