class EachValidator

Applies validator to every element of the collection

Public Instance Methods

validate_each(record, attribute, values) click to toggle source
# File lib/tram/validators/each_validator.rb, line 3
def validate_each(record, attribute, values)
  return unless values.is_a? Enumerable
  values.each_with_index do |value, index|
    item = record.dup.tap { |rec| rec.errors.clear }

    call_validations(item, attribute, value)

    copy_errors(item, record, attribute, index)
  end
end

Private Instance Methods

call_validations(item, attribute, value) click to toggle source
# File lib/tram/validators/each_validator.rb, line 16
def call_validations(item, attribute, value)
  Tram::Validators.validators(@attributes, options).each do |_, validator|
    validator.validate_each item, attribute, value
  end
end
copy_errors(item, record, attribute, index) click to toggle source
# File lib/tram/validators/each_validator.rb, line 22
def copy_errors(item, record, attribute, index)
  item.errors.messages.each do |original_key, messages|
    messages.each do |message|
      key = original_key.to_s.sub /\A#{attribute}/, "#{attribute}[#{index}]"
      record.errors.add key.to_sym, message
    end
  end
end