class Asset include Mongoid::Document include Mongoid::Timestamps mount_uploader :data, FileAssetUploader field :filename field :description has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy has_one :description, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy validates_presence_of :title, :data, :description belongs_to :asset_category belongs_to :assetable, polymorphic: true has_and_belongs_to_many :tags, :class_name => "AssetTag" before_save :set_key def title @title ||= I18nVariable.first(:conditions => {:key => 'title', :language_value_id => self.id, :language_value_type => self.class}) rescue nil end def description @description ||= I18nVariable.first(:conditions => {:key => 'description', :language_value_id => self.id, :language_value_type => self.class}) rescue nil end def sorted_tags tags.order_by(I18n.locale, :asc) end protected def set_key if title && title.new_record? title.key = 'title' end if description && description.new_record? description.key = 'description' end end end