class ImageActu < ActiveRecord::Base
  belongs_to :image_file

  accepts_nested_attributes_for :image_file
  
  has_many :image_actu_langs, :dependent => :destroy
  accepts_nested_attributes_for :image_actu_langs
  
  belongs_to :image_actu_album
  def lang(locale)
    self.image_actu_langs.find_by_lang_site_id(LangSite.find_by_slug(locale).id)
  end
  
  
  has_many :hashtaggings, :as =>:hashtaggable, :dependent => :destroy
  has_many :hashtags, :through => :hashtaggings
  before_validation do
    self.random_position = SecureRandom.hex(10) if !self.random_position?
  end
  after_save do
    self.hashtaggings.destroy_all
    self.image_actu_langs.each do |image_actu_lang|
      text = image_actu_lang.description.to_s
      text.scan(Hashtag::REGEX).each do |hashtag|
        hash_name = hashtag[0]
        if Hashtag.find_by_name(hash_name)
          hashtag = Hashtag.find_by_slug(hash_name.to_slug)
        else
          hashtag = Hashtag.create(:name =>hash_name)
          
        end
        
        self.hashtags << hashtag
      end
      
    end
    
  end
  
end
