Skip to content

Commit 1d364ee

Browse files
authored
MONGOID-5843 Ensure BSON::Decimal128 is considered to be numeric (#5961)
* 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 6fe2a5d commit 1d364ee

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

.mod/drivers-evergreen-tools

lib/mongoid/validatable.rb

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "mongoid/validatable/associated"
77
require "mongoid/validatable/format"
88
require "mongoid/validatable/length"
9+
require "mongoid/validatable/numericality"
910
require "mongoid/validatable/queryable"
1011
require "mongoid/validatable/presence"
1112
require "mongoid/validatable/uniqueness"

lib/mongoid/validatable/macros.rb

+15
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ def validates_length_of(*args)
8989
def validates_presence_of(*args)
9090
validates_with(PresenceValidator, _merge_attributes(args))
9191
end
92+
93+
# Validates whether or not a field contains a numeric value.
94+
#
95+
# @example
96+
# class Person
97+
# include Mongoid::Document
98+
# field :cost
99+
#
100+
# validates_numericality_of :cost
101+
# end
102+
#
103+
# @param [ Object... ] *args The names of the field(s) to validate.
104+
def validates_numericality_of(*args)
105+
validates_with(NumericalityValidator, _merge_attributes(args))
106+
end
92107
end
93108
end
94109
end
+19
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

+6
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def prepare_new_rails_app(name)
123123
end
124124

125125
context 'new application - rails' do
126+
before(:all) do
127+
if SpecConfig.instance.rails_version < '7.1'
128+
skip '`rails new` with rails < 7.1 fails because modern concurrent-ruby removed logger dependency'
129+
end
130+
end
131+
126132
it 'creates' do
127133
prepare_new_rails_app 'mongoid-test' do
128134
check_call(%w(rails g model post), env: clean_env)

spec/mongoid/validatable/numericality_spec.rb

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

0 commit comments

Comments
 (0)