Class: Mongoid::Validatable::NumericalityValidator
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
ActiveModel::Validations::NumericalityValidator
|
|
|
Instance Chain:
self,
ActiveModel::Validations::NumericalityValidator
|
|
| Inherits: |
ActiveModel::Validations::NumericalityValidator
|
| 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
-
#validate(record)
Reimplements EachValidator#validate in order to work around Mongoid’s nonstandard type-casting of
::Stringvalues. -
#prepare_value_for_validation(value, _record, _attr_name)
private
Ensure that
::BSON::Decimal128is treated as a BigDecimal during the validation step. -
#raw_value_for_validation(record, attr)
private
Fetches the raw value from the record, prior to any typecasting being applied.
Instance Method Details
#prepare_value_for_validation(value, _record, _attr_name) (private)
Ensure that ::BSON::Decimal128 is treated as a BigDecimal during the validation step.
# File 'lib/mongoid/validatable/numericality.rb', line 26
def prepare_value_for_validation(value, _record, _attr_name) value.is_a?(BSON::Decimal128) ? value.to_big_decimal : value end
#raw_value_for_validation(record, attr) (private)
Fetches the raw value from the record, prior to any typecasting being applied.
# File 'lib/mongoid/validatable/numericality.rb', line 32
def raw_value_for_validation(record, attr) attribute = record.database_field_name(attr) return nil if record.relations.key?(attribute) record.read_attribute_before_type_cast(attribute) end
#validate(record)
Reimplements EachValidator#validate in order to work around Mongoid’s nonstandard type-casting of ::String values.
# 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? && [:allow_nil]) || (value.blank? && [:allow_blank]) value = prepare_value_for_validation(value, record, attribute) validate_each(record, attribute, value) end end