Class: EmbeddedRecordsValidation

Inherits:
Validation show all
Defined in:
lib/yodel/models/core/validations/embedded_records_validation.rb

Instance Attribute Summary

Attributes inherited from Validation

#field, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Validation

#to_json

Constructor Details

#initialize(params, errors) ⇒ EmbeddedRecordsValidation

Returns a new instance of EmbeddedRecordsValidation.



2
3
4
5
# File 'lib/yodel/models/core/validations/embedded_records_validation.rb', line 2

def initialize(params, errors)
  super(params)
  @errors = errors
end

Class Method Details

.validate(field, records, record, errors) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yodel/models/core/validations/embedded_records_validation.rb', line 7

def self.validate(field, records, record, errors)
  # embedded record validations
  records = [records] unless records.respond_to?(:to_a)
  embedded_errors = Errors.new
  records.to_a.each_with_index do |embedded_record, index|
    embedded_errors[index] = embedded_record.valid? ? nil : embedded_record.errors
  end
  
  # field set validations
  field.fields.each do |name, embedded_field|
    next unless embedded_field.set_validations
    set_value = records.to_a.collect {|embedded| embedded.get(name)}.uniq
    embedded_field.set_validations.each do |type, params|
      Validation.validate(type, params, embedded_field, name, set_value, record, embedded_errors)
    end
  end
  
  errors[field.name] = embedded_errors unless embedded_errors.empty?
end

Instance Method Details

#describeObject



27
28
29
30
# File 'lib/yodel/models/core/validations/embedded_records_validation.rb', line 27

def describe
  # FIXME: don't just call inspect here, format correctly using describe calls
  "has these errors: #{@errors.inspect}}"
end