class PArticle < ApplicationRecord
  belongs_to :p_grade
  belongs_to :p_product_ref
  has_one :p_product, through: :p_product_ref
  has_one :p_product_color, through: :p_product_ref

  has_many :p_article_serial_nums, dependent: :destroy
  has_many :p_serial_num_values, through: :p_article_serial_nums
  accepts_nested_attributes_for :p_article_serial_nums

  validates_presence_of :p_product_ref
  # has_many :p_product_ref_specs, through: :p_product_ref
  # accepts_nested_attributes_for :p_product_ref_specs

  if PGrade::ACTIVATED
    acts_as_sorting :fields => {
      :id => {:name => "id", :reorder => true},
      :p_product_ref_code => {:name => "Code ref", :reorder => true},
      :p_product_ref => {:name => "Désignation", :reorder => true},
      :p_grade => {:name => "Grade", :reorder => true},
      :color => {:name => "Couleur"},
      :p_article_serial_nums => {:name => "N° identifiants"},
      :actions => {:name => "Actions", :reorder => false},
    }
  else
    acts_as_sorting :fields => {
      :id => {:name => "id", :reorder => true},
      :p_product_ref_code => {:name => "Code ref", :reorder => true},
      :p_product_ref => {:name => "Désignation", :reorder => true},
      :color => {:name => "Couleur"},
      :p_article_serial_nums => {:name => "N° identifiants"},
      :actions => {:name => "Actions", :reorder => false},
    }
  end
  def member_label
    "#{p_product_ref.cc_name}"
  end
end
