Module: ValidationReflection

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

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

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:



27
28
29
30
# File 'lib/validation_reflection.rb', line 27

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

#install(base) ⇒ Object

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/validation_reflection.rb', line 35

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