# encoding: utf-8

class ArchiveFile
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::MultiParameterAttributes

  PAYMENT_TYPES = @site_valid_locales

  field :title, localize: true
  
  has_and_belongs_to_many :tags, :class_name => "ArchiveTag"
  
  field :create_user_id
  field :update_user_id
  
  field :is_top, :type => Boolean, :default => false 
  field :is_hot, :type => Boolean, :default => false 
  field :is_hidden, :type => Boolean, :default => false 
  
  scope :can_display,where(is_hidden: false)
  
  belongs_to :archive_file_category

  has_many :archive_file_multiples, :autosave => true, :dependent => :destroy

  accepts_nested_attributes_for :archive_file_multiples, :allow_destroy => true
  
  validates :title, :at_least_one => true
  
  after_save :save_archive_file_multiples

  def self.search( category_id = nil )
  
	if category_id.to_s.size > 0

	  find(:all, :conditions => {archive_file_category_id: category_id}).desc( :is_top, :title )

	else
	  
	  find(:all).desc( :is_top, :title)
	  
	end
	
  end


  def self.widget_datas

	where( :is_hidden => false ).desc(:is_top, :title)

  end

  def is_top?
    self.is_top
  end
  
  def save_archive_file_multiples
	self.archive_file_multiples.each do |t|
	  if t.should_destroy
		t.destroy
	  end
    end
  end
  
end