class ProductCategory < ActiveRecord::Base
  acts_as_tree

  has_many :product_category_langs, :dependent => :destroy
  belongs_to :image_file
  
  belongs_to :image_file_left, :class_name => "ImageFile"
  belongs_to :image_file_right, :class_name => "ImageFile"
  
  belongs_to :top_image, :class_name => "ImageFile"

  accepts_nested_attributes_for :product_category_langs

  has_many :product_collections, :dependent => :destroy
  has_many :products, :dependent => :nullify

  def lang(lang)
    self.product_category_langs.find_by_lang(lang)
  end

  before_validation do
    self.parent_id = nil if self.parent_id == 0
    errors.add(:parent_id, 'doit être différent de la page actuelle') if parent_id == id and id != nil

    if self.id
      errors.add(:parent_id, 'attention la page parente à pour page parente celle-ci = boucle') if self.parent_id and self.id == ProductCategory.find(self.parent_id).parent_id
    end



  end


end
