class VideoFile < ActiveRecord::Base
  belongs_to :video_folder
  belongs_to :image_file
  belongs_to :investigation_cat
  
  has_many :video_file_langs, :dependent => :destroy
  accepts_nested_attributes_for :video_file_langs
  
  
  scope :recents, -> {where(:public => true).order("video_files.position ASC, video_files.created_at DESC")}
  
  scope :lang, lambda { |lang|
    if lang == "fr"
      where(:fr => true)
    else
      where(:en => true)
    end
      
      
  }
  
  
  
  
  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while DataFile.exists?(:token => self[:token])
  end
  
  before_create { generate_token() }
  
  mount_uploader :poster, ImageUploader
  mount_uploader :mpg, VideoUploader
  mount_uploader :ogg, VideoUploader
  mount_uploader :webm, VideoUploader
  
  def lang(locale)
    self.video_file_langs.find_by_lang_site_id(LangSite.find_by_slug(locale).id)
  end
    
end
