class MenuItemLang < ActiveRecord::Base
  belongs_to :lang_site
  belongs_to :menu_item
  validates :name, :presence => true #, :if => :force_validation?
  
  validates :slug, :presence => true
  belongs_to :image_file
  #def force_validation?
  #  true if visible or enabled
  #end
  
  def url
    self.permalink
    
  end
  
  before_validation do

    if !self.slug?

      self.slug = self.name

    end

    self.slug = self.slug.to_slug

    #self.validate_permalink

  end
  
  after_validation do
    if self.errors.empty?
      if self.permalink_changed?
        puts "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        puts self.changes["permalink"][0]
        puts self.lang_site.slug
        MenuItemSlugHistory.where(:locale => self.lang_site.slug, :permalink => self.changes["permalink"][0]).destroy_all
        MenuItemSlugHistory.create(:locale => self.lang_site.slug, :permalink => self.changes["permalink"][0], :menu_item_lang_id => self.id)
        puts "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
      
      
      end
    end
  end
    
    
end
