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

55 lines
1.9 KiB
Ruby
Raw Normal View History

2015-01-22 09:30:29 +08:00
module Mobile
module Entities
class Attachment < Grape::Entity
2015-06-19 15:05:25 +08:00
include Redmine::I18n
include ActionView::Helpers::NumberHelper
include ApplicationHelper
2015-01-22 09:30:29 +08:00
def self.attachment_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::Attachment)
if f.respond_to?(field)
2015-06-19 15:05:25 +08:00
if field == :created_on
format_time(f.send(field))
else
f.send(field)
end
2015-01-22 09:30:29 +08:00
else
2015-07-03 15:32:57 +08:00
case field
when :file_dir
"attachments/download/" << f.send(:id).to_s << '/'
when :attafile_size
(number_to_human_size(f.filesize)).gsub("ytes", "").to_s
when :coursename
f.course.nil? ? "" : f.course.name
2016-09-19 13:53:15 +08:00
when :syllabus_title
f.course.nil? ? "" : f.course.syllabus.nil? ? "" : f.course.syllabus.title
when :course_id
f.course.nil? ? 0 : f.course.id
2015-07-03 15:32:57 +08:00
end
2015-01-22 09:30:29 +08:00
end
end
end
end
2015-04-29 17:46:39 +08:00
attachment_expose :id
2015-01-22 09:30:29 +08:00
attachment_expose :filename
attachment_expose :description
2015-04-29 17:46:39 +08:00
attachment_expose :downloads
attachment_expose :quotes
2015-06-19 15:05:25 +08:00
attachment_expose :created_on
2015-07-03 15:32:57 +08:00
attachment_expose :file_dir
attachment_expose :attafile_size
attachment_expose :coursename #所属班级名
2016-09-19 13:53:15 +08:00
attachment_expose :syllabus_title #所属班级名
attachment_expose :course_id #所属班级名
expose :current_user_is_teacher, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
current_user_is_teacher = false
current_user_is_teacher = is_course_teacher(current_user,instance.course)
current_user_is_teacher
end
2015-01-22 09:30:29 +08:00
end
end
end