Module: ActiveRecordExtensions::ValidationReflection

Extended by:
ValidationReflection
Included in:
ValidationReflection, ClassMethods
Defined in:
lib/validation_reflection.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

CONFIG_PATH =

Look for config/initalizer here in:

::Dir.glob((::File.join(RAILS_ROOT, 'config', '**', 'validation_reflection.rb').to_s rescue '')).first || ''
CORE_VALIDATONS =
[
  :validates_acceptance_of,
  :validates_associated,
  :validates_confirmation_of,
  :validates_exclusion_of,
  :validates_format_of,
  :validates_inclusion_of,
  :validates_length_of,
  :validates_numericality_of,
  :validates_presence_of,
  :validates_uniqueness_of,
].freeze
@@reflected_validations =
CORE_VALIDATONS.dup
@@in_ignored_subvalidation =
false

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

:nodoc:



56
57
58
59
# File 'lib/validation_reflection.rb', line 56

def included(base) # :nodoc:
  return if base.kind_of?(::ActiveRecordExtensions::ValidationReflection::ClassMethods)
  base.extend(ClassMethods)
end

#install(base) ⇒ Object Also known as: reload

Iterate through all validations and store/cache the info for later easy access.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/validation_reflection.rb', line 77

def install(base)
  @@reflected_validations.each do |validation_type|
    next if base.respond_to?(:"#{validation_type}_with_reflection")
    ignore_subvalidations = false
    
    if validation_type.kind_of?(::Hash)
      ignore_subvalidations = validation_type[:ignore_subvalidations]
      validation_type = validation_type[:method]
    end
    
    base.class_eval %{
      class << self
        def #{validation_type}_with_reflection(*attr_names)
          ignoring_subvalidations(#{ignore_subvalidations}) do
            #{validation_type}_without_reflection(*attr_names)
            remember_validation_metadata(:#{validation_type}, *attr_names)
          end
        end
        alias_method_chain :#{validation_type}, :reflection
      end
    }, __FILE__, __LINE__
  end
end

#load_configObject

Load config/initializer on load, where ValidationReflection defaults (such as which validations to reflect upon) cane be overridden/extended.



64
65
66
67
68
69
70
71
72
# File 'lib/validation_reflection.rb', line 64

def load_config
  if ::File.file?(CONFIG_PATH)
    config = ::OpenStruct.new
    config.reflected_validations = @@reflected_validations
    silence_warnings do
      eval(::IO.read(CONFIG_PATH), binding, CONFIG_PATH)
    end
  end
end