class ChapterFile include Mongoid::Document include Mongoid::Timestamps mount_uploader :file, AssetUploader field :description, localize: true field :title, localize: true belongs_to :chapter def self.to_fronted(locale=I18n.locale) self.all.map{|file| file.to_fronted(locale)}.compact rescue [] end def to_fronted(locale=I18n.locale) file = self (!file[:file].blank?) ? { "file_url" => "/xhr/announcements/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 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 end