2020-07-22 02:23:36 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
class EventNewsFile
|
|
|
|
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
|
|
|
|
mount_uploader :file, AssetUploader
|
|
|
|
|
|
|
|
field :description, localize: true
|
|
|
|
field :title, localize: true
|
|
|
|
field :choose_lang, :type => Array, :default => ["en","zh_tw"]
|
2021-09-16 09:16:20 +00:00
|
|
|
field :privacy_type, type: String, default: 'public'
|
2020-07-22 02:23:36 +00:00
|
|
|
belongs_to :event_news
|
2021-09-16 09:16:20 +00:00
|
|
|
scope :to_fronted, ->(locale=I18n.locale){ self.map{|file| file.to_fronted(locale)}.compact rescue [] }
|
|
|
|
def to_fronted(locale=I18n.locale)
|
|
|
|
file = self
|
|
|
|
file.enabled_for?(locale) && !file[:file].blank? ? { "file_url" => "/xhr/event_news/file/#{file.id}/#{file['file']}" + "\" title=\"#{file.file_title}",
|
|
|
|
"file_title" => (file.title.blank? ? URI.unescape(File.basename(file.file.path)) : file.title rescue '')
|
|
|
|
} : nil rescue nil
|
|
|
|
end
|
2020-07-22 02:23:36 +00:00
|
|
|
|
2021-09-16 09:16:20 +00:00
|
|
|
def file_title
|
|
|
|
if self.description.present?
|
|
|
|
return self.description
|
|
|
|
elsif self.title.present?
|
|
|
|
return self.title
|
|
|
|
else
|
|
|
|
return File.basename(self.file.path)
|
|
|
|
end
|
|
|
|
end
|
2020-07-22 02:23:36 +00:00
|
|
|
def enabled_for?(lang)
|
|
|
|
if lang.nil?
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return self.choose_lang.include?(lang)
|
|
|
|
end
|
|
|
|
end
|
2021-09-16 09:16:20 +00:00
|
|
|
def can_access?(user)
|
|
|
|
if user.nil? && self.privacy_type == 'logged_in'
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2020-07-22 02:23:36 +00:00
|
|
|
end
|