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
|
2016-04-01 20:46:38 +08:00
|
|
|
include ApiHelper
|
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-06-30 15:17:22 +08:00
|
|
|
elsif f.is_a?(::News)
|
2016-03-30 17:49:05 +08:00
|
|
|
if f.respond_to?(field)
|
|
|
|
if field == :created_on
|
|
|
|
format_time(f.send(field))
|
|
|
|
else
|
|
|
|
f.send(field)
|
|
|
|
end
|
2015-06-30 15:17:22 +08:00
|
|
|
else
|
2016-03-30 17:49:05 +08:00
|
|
|
case field
|
|
|
|
when :course_name
|
2016-04-01 20:46:38 +08:00
|
|
|
get_course(f.course_id).name unless f.course_id == nil
|
2016-04-07 16:40:21 +08:00
|
|
|
when :praise_count
|
2016-04-01 20:46:38 +08:00
|
|
|
get_activity_praise_num(f)
|
2016-04-07 16:40:21 +08:00
|
|
|
when :act_type
|
|
|
|
'News'
|
|
|
|
when :act_id
|
|
|
|
f.id
|
2016-07-22 15:37:25 +08:00
|
|
|
when :comment_count
|
2016-07-28 17:26:57 +08:00
|
|
|
f.comments.count
|
2016-03-30 17:49:05 +08:00
|
|
|
end
|
2015-06-30 15:17:22 +08:00
|
|
|
end
|
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|
|
2015-07-03 09:44:26 +08:00
|
|
|
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
|
2015-01-20 17:42:18 +08:00
|
|
|
end
|
2016-04-07 16:40:21 +08:00
|
|
|
news_expose :act_type
|
|
|
|
news_expose :act_id
|
2015-01-20 15:17:02 +08:00
|
|
|
#作者id
|
|
|
|
news_expose :author_id
|
|
|
|
#作者名
|
|
|
|
news_expose :author_name
|
2015-06-26 17:16:03 +08:00
|
|
|
#作者头像url
|
|
|
|
news_expose :author_img_url
|
2015-01-20 15:17:02 +08:00
|
|
|
#新闻内容
|
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
|
|
|
#评论数量
|
2016-07-22 15:37:25 +08:00
|
|
|
news_expose :comment_count
|
2016-04-07 16:40:21 +08:00
|
|
|
news_expose :praise_count
|
2016-03-30 17:49:05 +08:00
|
|
|
#课程名字
|
|
|
|
news_expose :course_name
|
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]
|
2015-07-08 17:36:17 +08:00
|
|
|
elsif f.is_a?(::News) && f.respond_to?(:comments)
|
2016-04-07 15:09:17 +08:00
|
|
|
f.comments.reverse
|
2015-02-04 11:57:37 +08:00
|
|
|
end
|
|
|
|
end
|
2016-04-07 11:16:38 +08:00
|
|
|
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
|
|
|
|
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
|
2015-01-20 15:17:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|