module Enumerize::Base
Public Class Methods
included(base)
click to toggle source
# File lib/enumerize/base.rb, line 5 def self.included(base) base.extend ClassMethods base.singleton_class.prepend ClassMethods::Hook if base.respond_to?(:validate) base.validate :_validate_enumerized_attributes end end
new(*)
click to toggle source
Calls superclass method
# File lib/enumerize/base.rb, line 52 def initialize(*) super _set_default_value_for_enumerized_attributes end
Public Instance Methods
read_attribute_for_validation(key)
click to toggle source
Calls superclass method
# File lib/enumerize/base.rb, line 57 def read_attribute_for_validation(key) key = key.to_s if _enumerized_values_for_validation.has_key?(key) _enumerized_values_for_validation[key] elsif defined?(super) super else send(key) end end
Private Instance Methods
_enumerized_values_for_validation()
click to toggle source
# File lib/enumerize/base.rb, line 71 def _enumerized_values_for_validation @_enumerized_values_for_validation ||= {} end
_set_default_value_for_enumerized_attributes()
click to toggle source
# File lib/enumerize/base.rb, line 91 def _set_default_value_for_enumerized_attributes self.class.enumerized_attributes.each do |attr| next if attr.default_value.nil? begin if respond_to?(attr.name) attr_value = public_send(attr.name) else next end value_for_validation = _enumerized_values_for_validation[attr.name.to_s] if (!attr_value || attr_value.empty?) && (!value_for_validation || value_for_validation.empty?) value = Utils.call_if_callable(attr.default_value, self) public_send("#{attr.name}=", value) end rescue ActiveModel::MissingAttributeError end end end
_validate_enumerized_attributes()
click to toggle source
# File lib/enumerize/base.rb, line 75 def _validate_enumerized_attributes self.class.enumerized_attributes.each do |attr| skip_validations = Utils.call_if_callable(attr.skip_validations_value, self) next if skip_validations value = read_attribute_for_validation(attr.name) next if value.blank? if attr.kind_of? Multiple errors.add attr.name unless value.respond_to?(:all?) && value.all? { |v| v.blank? || attr.find_value(v) } else errors.add attr.name, :inclusion unless attr.find_value(value) end end end