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

85 lines
2.9 KiB
Ruby
Raw Normal View History

2016-03-30 10:32:45 +08:00
# encoding: utf-8
module Mobile
module Entities
class Whomework <Grape::Entity
include ApiHelper
include ApplicationHelper
include Redmine::I18n
def self.whomework_expose(f)
expose f do |wh, opt|
if wh.is_a?(Hash) && wh.key?(f)
2016-03-30 17:49:05 +08:00
wh[f]
2016-03-30 10:32:45 +08:00
elsif wh.is_a?(::HomeworkCommon)
if wh.respond_to?(f)
2016-03-30 17:49:05 +08:00
if f == :created_at
format_time(wh.send(f))
else
wh.send(f)
end
2016-03-30 10:32:45 +08:00
else
case f
when :absence_penalty
wh.nil? || wh.homework_detail_manual.nil? ? 0 : wh.homework_detail_manual.absence_penalty
when :evaluation_start
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_start, 0)
when :evaluation_end
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_end, 1)
2016-04-07 16:40:21 +08:00
when :praise_count
2016-03-30 14:02:35 +08:00
get_activity_praise_num(wh)
when :whomework_journal_count
wh.journals_for_messages.count
2016-03-30 17:49:05 +08:00
when :course_name
wh.course.name
2016-04-07 16:40:21 +08:00
when :act_type
2016-04-07 16:53:37 +08:00
'HomeworkCommon'
2016-04-07 16:40:21 +08:00
when :act_id
wh.id
2016-03-30 10:32:45 +08:00
end
end
end
end
end
2016-03-30 14:02:35 +08:00
expose :author, using: Mobile::Entities::User do |w, opt|
if w.is_a?(::HomeworkCommon)
w.user
end
end
2016-04-01 18:23:12 +08:00
expose :current_user, using: Mobile::Entities::User do |w, opt|
current_user
end
2016-03-30 10:32:45 +08:00
expose :name
expose :description
expose :publish_time
expose :end_time
expose :homework_type
expose :late_penalty
expose :course_id
expose :anonymous_comment
expose :quotes
expose :is_open
2016-04-07 16:40:21 +08:00
whomework_expose :act_type
whomework_expose :act_id
2016-03-30 17:49:05 +08:00
whomework_expose :course_name
2016-03-30 10:32:45 +08:00
whomework_expose :created_at
whomework_expose :absence_penalty
whomework_expose :evaluation_start
whomework_expose :evaluation_end
2016-04-07 16:40:21 +08:00
whomework_expose :praise_count
2016-03-30 14:02:35 +08:00
whomework_expose :whomework_journal_count
2016-03-30 15:51:27 +08:00
expose :journals_for_messages, using: Mobile::Entities::Jours do |f, opt|
#f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
if f.is_a?(::HomeworkCommon)
2016-04-07 15:09:17 +08:00
f.journals_for_messages.reverse
2016-03-30 15:51:27 +08:00
end
2016-03-30 10:32:45 +08:00
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
2016-03-30 10:32:45 +08:00
end
end
end