class NewsBulletinFile

  include Mongoid::Document
  include Mongoid::Timestamps

  mount_uploader :file, AssetUploader
  
  # field :filetitle
  # field :description
  has_one :filetitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
  has_one :description, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
  
  # field :to_save, :type => Boolean
  field :should_destroy, :type => Boolean
  
  belongs_to :news_bulletin
  # embedded_in :news_bulletin
  
  before_save :set_key
  
  def filetitle
    @filetitle ||= I18nVariable.first(:conditions => {:key => 'filetitle', :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
  
  protected
  
  def set_key
    if filetitle && filetitle.new_record?
      filetitle.key = 'filetitle'
    end
    if description && description.new_record?
      description.key = 'description'
    end
  end  

end
