Class: ReceiverValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/receiver_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/validators/receiver_validator.rb', line 5

def validate_each(record, attribute, value)
  if Array.wrap(value).reject {|r| r.marked_for_destruction? || r.valid?(record.validation_context) }.any?
    receiver = record.public_method(attribute).call
    if options[:map_attributes]
      options[:map_attributes].each do |receiver_attribute, command_attribute|
        if errors = receiver.errors.delete(receiver_attribute)
          errors.each do |error|
            record.errors.add(command_attribute, error)
          end
        end
      end
    end
    unless receiver.errors.empty?
      record.errors.add(attribute, :invalid, options.merge(value: value))
    end
  end
end