Class: Mongoid::Validatable::NumericalityValidator

Inherits:
ActiveModel::Validations::NumericalityValidator
  • Object
show all
Defined in:
lib/mongoid/validatable/numericality.rb

Overview

A specialization of the ActiveModel numericality validator, which adds logic to recognize and accept BSON::Decimal128 as a number.

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object

Reimplements EachValidator#validate in order to work around Mongoid's nonstandard type-casting of String values.

Parameters:

  • record (Document)

    The document to validate.



12
13
14
15
16
17
18
19
20
# File 'lib/mongoid/validatable/numericality.rb', line 12

def validate(record)
  attributes.each do |attribute|
    value = raw_value_for_validation(record, attribute)
    next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])

    value = prepare_value_for_validation(value, record, attribute)
    validate_each(record, attribute, value)
  end
end