Class: ArrayValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/array_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, values) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/validators/array_validator.rb', line 2

def validate_each(record, attribute, values)
  Array(values).each do |value|
    options.each do |key, args|
      validator_options = {attributes: attribute}
      validator_options.merge!(args) if args.is_a?(Hash)
      next if value.nil? && validator_options[:allow_nil]

      next if value.blank? && validator_options[:allow_blank]

      validator_class_name = "#{key.to_s.camelize}Validator"
      validator_class = begin
        validator_class_name.constantize
      rescue NameError
        "ActiveModel::Validations::#{validator_class_name}".constantize
      end

      validator = validator_class.new(validator_options)
      validator.validate_each(record, attribute, value)
    end
  end
end