Skip to content

Commit 699e087

Browse files
committed
MONGOID-5222 fix BigDecimal mongoization
1 parent d5d217d commit 699e087

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

lib/mongoid/extensions/big_decimal.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,22 @@ def demongoize(object)
6565
# @return [ String | BSON::Decimal128 | nil ] A String or Decimal128
6666
# representing the object or nil.
6767
def mongoize(object)
68+
return nil if object.is_a?(String) && object.blank?
6869
_mongoid_wrap_mongoize(object) do
69-
if object.is_a?(BSON::Decimal128)
70-
object
71-
elsif Mongoid.map_big_decimal_to_decimal128
72-
if object.is_a?(BigDecimal)
70+
if Mongoid.map_big_decimal_to_decimal128
71+
if object.is_a?(BSON::Decimal128)
72+
object
73+
elsif object.is_a?(BigDecimal)
7374
BSON::Decimal128.new(object)
74-
elsif object.numeric?
75-
BSON::Decimal128.new(object.to_s)
75+
elsif object.respond_to?(:to_d)
76+
BSON::Decimal128.new(object.to_d)
77+
end
78+
else
79+
if object.is_a?(BSON::Decimal128) || object.numeric?
80+
object.to_s
81+
elsif object.respond_to?(:to_d)
82+
object.to_d.to_s
7683
end
77-
elsif object.numeric?
78-
object.to_s
7984
end
8085
end
8186
end

lib/mongoid/extensions/float.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module ClassMethods
3636
#
3737
# @return [ String ] The object mongoized.
3838
def mongoize(object)
39-
return nil if object.to_s.blank?
39+
return nil if object.is_a?(String) && object.blank?
4040
_mongoid_wrap_mongoize(object) do
4141
if object.respond_to?(:to_f)
4242
object.to_f

lib/mongoid/extensions/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module ClassMethods
4444
#
4545
# @return [ String ] The object mongoized.
4646
def mongoize(object)
47-
return nil if object.to_s.blank?
47+
return nil if object.is_a?(String) && object.blank?
4848
_mongoid_wrap_mongoize(object) do
4949
if object.respond_to?(:to_i)
5050
object.to_i

spec/mongoid/extensions/big_decimal_spec.rb

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
let(:mongoized) do
196196
BigDecimal.mongoize(value)
197197
end
198+
198199
context "when the value is a BigDecimal" do
199200

200201
let(:value) do
@@ -234,10 +235,8 @@
234235
""
235236
end
236237

237-
it "raises an error" do
238-
expect do
239-
mongoized
240-
end.to raise_error(Mongoid::Errors::InvalidValue)
238+
it "returns nil" do
239+
expect(mongoized).to be_nil
241240
end
242241
end
243242

@@ -269,10 +268,8 @@
269268
"1a2"
270269
end
271270

272-
it "raises an error" do
273-
expect do
274-
mongoized
275-
end.to raise_error(Mongoid::Errors::InvalidValue)
271+
it "returns a string" do
272+
expect(mongoized).to eq("1.0")
276273
end
277274
end
278275

@@ -432,6 +429,16 @@
432429
expect(mongoized).to eq("-Infinity")
433430
end
434431
end
432+
433+
context "when the value is a decimal128" do
434+
let(:value) do
435+
BSON::Decimal128.new("42")
436+
end
437+
438+
it "returns a String" do
439+
expect(mongoized).to eq("42")
440+
end
441+
end
435442
end
436443

437444
describe "#mongoize" do
@@ -670,10 +677,8 @@
670677
""
671678
end
672679

673-
it "raises an error" do
674-
expect do
675-
mongoized
676-
end.to raise_error(Mongoid::Errors::InvalidValue)
680+
it "returns nil" do
681+
expect(mongoized).to be_nil
677682
end
678683
end
679684

@@ -705,10 +710,8 @@
705710
"1a2"
706711
end
707712

708-
it "raises an error" do
709-
expect do
710-
mongoized
711-
end.to raise_error(Mongoid::Errors::InvalidValue)
713+
it "returns a decimal128" do
714+
expect(mongoized).to eq(BSON::Decimal128.new("1"))
712715
end
713716
end
714717

@@ -869,6 +872,16 @@
869872
expect(mongoized).to eq(BSON::Decimal128.new("-Infinity"))
870873
end
871874
end
875+
876+
context "when the value is a decimal128" do
877+
let(:value) do
878+
BSON::Decimal128.new("42")
879+
end
880+
881+
it "returns a String" do
882+
expect(mongoized).to eq(value)
883+
end
884+
end
872885
end
873886

874887
describe "#mongoize" do

0 commit comments

Comments
 (0)