class InformationStandardCategory < ActiveRecord::Base
  
  attr_accessible :name, :information_standard_ids

  ##
  # Non database attributes
  
  attr_accessible :information_standard_infos_attributes

  # Associations
  has_many :institutions
  has_many :information_standard_infos, dependent: :destroy
  accepts_nested_attributes_for :information_standard_infos, allow_destroy: true
  has_many :information_standards, through: :information_standard_infos, order: :priority
  has_many :information_rankings

  # Validations
  validates :name, presence: true

  scope :with_published_information_ranking, where('id IN (SELECT information_standard_category_id FROM information_rankings WHERE published = 1)')

  before_save :destroy_empty_infos
  
  def destroy_empty_infos
    information_standard_infos.each do |info|
      #info.mark_for_destruction if info.percent.to_f == 0.0
      info.mark_for_destruction if info.published == 0
    end
  end
end
