orbit-basic/app/models/asset.rb

46 lines
1.2 KiB
Ruby
Raw Normal View History

2010-02-01 09:24:28 +00:00
class Asset
include Mongoid::Document
2011-03-08 09:25:46 +00:00
include Mongoid::Timestamps
2010-02-01 09:24:28 +00:00
2012-05-17 01:30:21 +00:00
mount_uploader :data, FileAssetUploader
2010-02-01 09:24:28 +00:00
field :filename
field :description
2012-05-17 10:31:40 +00:00
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
2010-02-01 09:24:28 +00:00
2012-05-17 10:31:40 +00:00
validates_presence_of :title, :data, :description
2012-05-16 03:07:11 +00:00
belongs_to :asset_category
belongs_to :assetable, polymorphic: true
2012-05-16 22:28:29 +00:00
has_and_belongs_to_many :tags, :class_name => "AssetTag"
2012-05-17 10:31:40 +00:00
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
2012-05-16 22:28:29 +00:00
def sorted_tags
tags.order_by(I18n.locale, :asc)
end
2012-05-17 10:31:40 +00:00
protected
def set_key
if title && title.new_record?
2012-05-17 10:31:40 +00:00
title.key = 'title'
end
if description && description.new_record?
2012-05-17 10:31:40 +00:00
description.key = 'description'
end
end
2010-02-05 08:15:16 +00:00
end