Class: ActiveData::Model::Validations::NestedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_data/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



25
26
27
28
29
30
# File 'lib/active_data/model/validations/nested.rb', line 25

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

.validate_nested(record, name, value) ⇒ Object



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

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



34
35
36
37
38
# File 'lib/active_data/model/validations/nested.rb', line 34

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