Class: NestedValidation::NestedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/models/concerns/nested_validation.rb

Overview

Validates associated records and propagates the errors back onto the parent object

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ NestedValidator

Returns a new instance of NestedValidator.



26
27
28
29
# File 'app/models/concerns/nested_validation.rb', line 26

def initialize(options)
  @flatten_keys = options.fetch(:flatten_keys, true)
  super
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'app/models/concerns/nested_validation.rb', line 31

def validate_each(record, attribute, value)
  case value
  when Array
    value.each_with_index { |nested, index| validate_one(nested, record, attribute, index) }
  when nil
    nil # Do nothing
  else
    validate_one(value, record, attribute)
  end
end