Class: ActiveLayer::Validations::EachValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_layer/validations/each_validator.rb

Overview

This class hooks into the standard validations and allows the new ActiveLayer::Validations to function as a NestedValidator

  class MyValidator
     include AciveLayer::Validations
     validates MyOtherValidator, :attributes => :sub_models
  end

In the above case this class will be used to adapt the MyOtherValidator to look like an ActiveModel::Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_layer_validatorObject

Returns the value of attribute active_layer_validator.



15
16
17
# File 'lib/active_layer/validations/each_validator.rb', line 15

def active_layer_validator
  @active_layer_validator
end

Instance Method Details

#validate_each(parent_validations, attribute, value) ⇒ Object

The parent_validations will be the parent class that has mixed in the ActiveLayer::Validations module



20
21
22
23
24
25
26
27
28
# File 'lib/active_layer/validations/each_validator.rb', line 20

def validate_each(parent_validations, attribute, value)
  nested_attribute = parent_validations.read_attribute_for_validation(attribute)

  Array.wrap(nested_attribute).each do |element|
    active_layer_validator.active_layer_object = element
    active_layer_validator.valid?
    parent_validations.merge_errors(active_layer_validator.errors, "#{attribute}.")
  end
end