Sha256: 08fa16988a964354b0835a7c8151a085d9e7ce9ecb633ecdf605b1ce3d9aeaea

Contents?: true

Size: 1.84 KB

Versions: 6

Compression:

Stored size: 1.84 KB

Contents

module Discerner
  module Methods
    module Models
      module ParameterValueCategory
        def self.included(base)
          base.send :include, SoftDelete

          # Associations
          base.send :belongs_to, :parameter,                       :inverse_of => :parameter_value_categories
          base.send :has_many,   :parameter_value_categorizations, :dependent => :destroy
          base.send :has_many,   :parameter_values, :through => :parameter_value_categorizations

          # Scopes
          base.send(:scope, :ordered_by_name, -> { base.order('discerner_parameter_value_categories.name ASC') })
          base.send(:scope, :ordered_by_display_order_and_name, -> { base.order('discerner_parameter_value_categories.display_order ASC, discerner_parameter_value_categories.name ASC') })

          # Validations
          base.send :validates_presence_of, :parameter, :unique_identifier, :name
          base.send :validates, :unique_identifier, :uniqueness => {:scope => [:parameter_id, :deleted_at], :message => "for parameter value category has already been taken"}
          base.send :validate, :parameter_value_belongs_to_parameter

          # Whitelisting attributes
          base.send :attr_accessible, :parameter, :parameter_id, :unique_identifier, :name
        end

        def css_class
          "parameter_value_category_#{parameterized_name}"
        end

        def parameterized_name
          name.parameterize.underscore
        end

        private
          def parameter_value_belongs_to_parameter
            unless parameter.blank?
              parameter_values.each do |parameter_value|
                errors.add(:base,"Parameter value #{parameter_value.name} does not belong to parameter #{parameter.name}") unless parameter_value.parameter_id == parameter.id
              end
            end
          end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
discerner-1.2.2 lib/discerner/methods/models/parameter_value_category.rb
discerner-1.2.1 lib/discerner/methods/models/parameter_value_category.rb
discerner-1.2.0 lib/discerner/methods/models/parameter_value_category.rb
discerner-1.1.20 lib/discerner/methods/models/parameter_value_category.rb
discerner-1.1.19 lib/discerner/methods/models/parameter_value_category.rb
discerner-1.1.18 lib/discerner/methods/models/parameter_value_category.rb