Class: CouchbaseOrm::EmbeddedAssociatedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/couchbase-orm/validators/embedded_associated_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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/couchbase-orm/validators/embedded_associated_validator.rb', line 5

def validate_each(record, attribute, value)
  if value.is_a?(Array)
    value.each_with_index do |v, i|
      next if v.valid?

      record.errors.add(attribute, "item ##{i} is invalid")
      v.errors.each do |*args|
        if args.first.respond_to?(:attribute) # rails 7
          error = args.first
          key = "#{attribute}_#{i}_#{error.attribute}".to_sym
          msg = error.message
        else # Rails 5/6
          k, msg = args
          key = "#{attribute}_#{i}_#{k}".to_sym
        end
        record.errors.add(key, msg)
      end
    end
  else
    return if value.nil? || value.valid?

    record.errors.add(attribute, 'is invalid')
    value.errors.each do |*args|
      if args.first.respond_to?(:attribute) # rails 7
        error = args.first
        key = "#{attribute}_#{error.attribute}".to_sym
        msg = error.message
      else # Rails 5/6
        k, msg = args
        key = "#{attribute}_#{k}".to_sym
      end
      record.errors.add(key, msg)
    end
  end
end