Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .mod/drivers-evergreen-tools
1 change: 1 addition & 0 deletions lib/mongoid/validatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "mongoid/validatable/associated"
require "mongoid/validatable/format"
require "mongoid/validatable/length"
require "mongoid/validatable/numericality"
require "mongoid/validatable/queryable"
require "mongoid/validatable/presence"
require "mongoid/validatable/uniqueness"
Expand Down
15 changes: 15 additions & 0 deletions lib/mongoid/validatable/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ def validates_length_of(*args)
def validates_presence_of(*args)
validates_with(PresenceValidator, _merge_attributes(args))
end

# Validates whether or not a field contains a numeric value.
#
# @example
# class Person
# include Mongoid::Document
# field :cost
#
# validates_numericality_of :cost
# end
#
# @param [ Object... ] *args The names of the field(s) to validate.
def validates_numericality_of(*args)
validates_with(NumericalityValidator, _merge_attributes(args))
end
end
end
end
19 changes: 19 additions & 0 deletions lib/mongoid/validatable/numericality.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Mongoid
module Validatable
# A specialization of the ActiveModel numericality validator, which adds
# logic to recognize and accept BSON::Decimal128 as a number.
class NumericalityValidator < ActiveModel::Validations::NumericalityValidator
private

# Ensure that BSON::Decimal128 is treated as a BigDecimal during the
# validation step.
def prepare_value_for_validation(value, record, attr_name)
result = super

result.is_a?(BSON::Decimal128) ? result.to_big_decimal : result
end
end
end
end
6 changes: 6 additions & 0 deletions spec/integration/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def prepare_new_rails_app(name)
end

context 'new application - rails' do
before(:all) do
if SpecConfig.instance.rails_version < '7.1'
skip '`rails new` with rails < 7.1 fails because modern concurrent-ruby removed logger dependency'
end
end

it 'creates' do
prepare_new_rails_app 'mongoid-test' do
check_call(%w(rails g model post), env: clean_env)
Expand Down
16 changes: 16 additions & 0 deletions spec/mongoid/validatable/numericality_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,21 @@ class TestModel
expect(model).to_not be_valid
end
end

context 'when the value is numeric' do
let(:model) { TestModel.new(amount: '15.0') }

it 'returns true' do
expect(model).to be_valid
end
end

context 'when the value is a BSON::Decimal128' do
let(:model) { TestModel.new(amount: BSON::Decimal128.new('15.0')) }

it 'returns true' do
expect(model).to be_valid
end
end
end
end
Loading