Class: Granite::Form::Model::Validations::NestedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/granite/form/model/validations/nested.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import_errors(from, to, prefix) ⇒ Object

up to 6.0.x



27
28
29
30
31
32
# File 'lib/granite/form/model/validations/nested.rb', line 27

def self.import_errors(from, to, prefix)
  from.each do |error|
    key = "#{prefix}.#{error.attribute}"
    to.import(error, attribute: key) unless to.messages_for(key).include?(error.message)
  end
end

.validate_nested(record, name, value) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/granite/form/model/validations/nested.rb', line 8

def self.validate_nested(record, name, value)
  if value.is_a?(Enumerable)
    value.each.with_index do |object, i|
      import_errors(object.errors, record.errors, "#{name}.#{i}") if yield object
    end
  elsif value
    import_errors(value.errors, record.errors, name.to_s) if yield value
  end
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



36
37
38
39
40
# File 'lib/granite/form/model/validations/nested.rb', line 36

def validate_each(record, attribute, value)
  self.class.validate_nested(record, attribute, value) do |object|
    object.invalid? && !(object.respond_to?(:marked_for_destruction?) && object.marked_for_destruction?)
  end
end