Class: Torque::PostgreSQL::Validations::NestedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/torque/postgresql/validations.rb

Overview

Checks if the objects nested into an attribute are valid, adding a single error to the attribute when any of them is not. It takes the regular allow_nil and allow_blank options, which is how values that are merely absent are left out of it

Example:

validates :home, nested: true
validates :settings, nested: true, allow_blank: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/torque/postgresql/validations.rb', line 16

def validate_each(record, attribute, value)
  invalid = Array.wrap(value).any? do |item|
    item.respond_to?(:invalid?) && item.invalid?
  end

  record.errors.add(attribute, :invalid) if invalid
end