Skip to content

Commit 1f975dd

Browse files
authored
MONGOID-5843 Ensure BSON::Decimal128 is considered to be numeric (mongodb#5961) (mongodb#5964)
* RUBY-5843 Ensure BSON::Decimal128 is considered to be numeric * bump drivers-evergreen-tools * let's see how this version of drivers-evergreen-tools does * Revert "let's see how this version of drivers-evergreen-tools does" This reverts commit 243fc49. * don't try to run `rails new` on rails < 7.1 concurrent-ruby gem removed a dependency on logger, which rails assumed was there. Installing those versions of Rails will install a too-new version of concurrent-ruby, which causes `rails new` to fail because it can't resolve Logger.
1 parent 567c2fe commit 1f975dd

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

.mod/drivers-evergreen-tools

lib/mongoid/validatable.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "mongoid/validatable/associated"
66
require "mongoid/validatable/format"
77
require "mongoid/validatable/length"
8+
require "mongoid/validatable/numericality"
89
require "mongoid/validatable/queryable"
910
require "mongoid/validatable/presence"
1011
require "mongoid/validatable/uniqueness"

lib/mongoid/validatable/macros.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ def validates_length_of(*args)
8484
def validates_presence_of(*args)
8585
validates_with(PresenceValidator, _merge_attributes(args))
8686
end
87+
88+
# Validates whether or not a field contains a numeric value.
89+
#
90+
# @example
91+
# class Person
92+
# include Mongoid::Document
93+
# field :cost
94+
#
95+
# validates_numericality_of :cost
96+
# end
97+
#
98+
# @param [ Object... ] *args The names of the field(s) to validate.
99+
def validates_numericality_of(*args)
100+
validates_with(NumericalityValidator, _merge_attributes(args))
101+
end
87102
end
88103
end
89104
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
module Mongoid
4+
module Validatable
5+
# A specialization of the ActiveModel numericality validator, which adds
6+
# logic to recognize and accept BSON::Decimal128 as a number.
7+
class NumericalityValidator < ActiveModel::Validations::NumericalityValidator
8+
private
9+
10+
# Ensure that BSON::Decimal128 is treated as a BigDecimal during the
11+
# validation step.
12+
def prepare_value_for_validation(value, record, attr_name)
13+
result = super
14+
15+
result.is_a?(BSON::Decimal128) ? result.to_big_decimal : result
16+
end
17+
end
18+
end
19+
end

spec/integration/app_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def start_app(cmd, port, timeout)
9595
end
9696

9797
context 'new application - rails' do
98+
before(:all) do
99+
if SpecConfig.instance.rails_version < '7.1'
100+
skip '`rails new` with rails < 7.1 fails because modern concurrent-ruby removed logger dependency'
101+
end
102+
end
103+
98104
it 'creates' do
99105
install_rails
100106

spec/mongoid/validatable/numericality_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,21 @@ class TestModel
2828
expect(model).to_not be_valid
2929
end
3030
end
31+
32+
context 'when the value is numeric' do
33+
let(:model) { TestModel.new(amount: '15.0') }
34+
35+
it 'returns true' do
36+
expect(model).to be_valid
37+
end
38+
end
39+
40+
context 'when the value is a BSON::Decimal128' do
41+
let(:model) { TestModel.new(amount: BSON::Decimal128.new('15.0')) }
42+
43+
it 'returns true' do
44+
expect(model).to be_valid
45+
end
46+
end
3147
end
3248
end

0 commit comments

Comments
 (0)