Class: ActiveModelValidators::NumericArrayValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_model_validators/numeric_array_validator.rb

Overview

Validator of numerical array

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Adds error if there an attribute with not a numerical array provided

  • record - ActiveRecord model

  • attr - model attribute to store an array

  • value - value, supposed to be a numerical array

Example

class MyModel < ActiveRecord::Base
  validates :my_atribute, :'active_model_validators/numeric_array' => true
end

my_model_instance = MyModel.create(my_attribute: [1,2,3])
my_model_instance.valid? # => true
my_model_instance.my_attribute = 'bar'
my_model_instance.valid? # => false


21
22
23
24
25
# File 'lib/active_model_validators/numeric_array_validator.rb', line 21

def validate_each(record, attribute, value)
  unless array_numeric?(value)
    record.errors.add(attribute, options[:message] || numeric_message)
  end
end