Skip to content

Commit 7ec03ec

Browse files
MONGOID-5507 - Fix two bugs in BigDecimal mongoization (#5485)
* Fix two bugs in BigDecimal mongoization 1. demongoize - has redundant elsif object.numeric? 2. mongoize - missing to_s in return condition when map_big_decimal_to_decimal128 is false * Remove obsolete comment * Update big_decimal.rb * Fix spec * MONGOID-5507 add release notes and minor changes Co-authored-by: shields <[email protected]> Co-authored-by: Neil Shweky <[email protected]>
1 parent 604a407 commit 7ec03ec

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

docs/release-notes/mongoid-9.0.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,14 @@ Mongoid 9.0 flips the default of this flag from ``true`` => ``false``.
120120

121121
This means that, by default, Mongoid 9 will update the existing document and
122122
will not replace it.
123+
124+
125+
Bug Fixes and Improvements
126+
--------------------------
127+
128+
This section will be for smaller bug fixes and improvements:
129+
130+
- With the ``map_big_decimal_to_decimal128`` flag set to false, ``demongoizing``
131+
a non-numeric, non-string value that implements ``:to_d`` will return a string
132+
rather than a ``BigDecimal``
133+
`MONGOID-5507 <https://jira.mongodb.org/browse/MONGOID-5507>`_.

lib/mongoid/criteria/queryable/extensions/big_decimal.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def evolve(object)
3131
else
3232
obj.to_s
3333
end
34-
# Always return on string for backwards compatibility with querying
35-
# string-backed BigDecimal fields.
3634
when BSON::Decimal128 then obj
3735
else
3836
if obj.numeric?

lib/mongoid/extensions/big_decimal.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ def demongoize(object)
4646
return if object.blank?
4747
if object.is_a?(BSON::Decimal128)
4848
object.to_big_decimal
49-
elsif object.numeric?
50-
BigDecimal(object.to_s)
5149
elsif object.numeric?
5250
object.to_d
5351
end
@@ -79,7 +77,7 @@ def mongoize(object)
7977
if object.is_a?(BSON::Decimal128) || object.numeric?
8078
object.to_s
8179
elsif !object.is_a?(String)
82-
object.try(:to_d)
80+
object.try(:to_d)&.to_s
8381
end
8482
end
8583
end

spec/mongoid/extensions/big_decimal_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@
283283
expect(value).to be_a(ActiveSupport::Duration)
284284
end
285285

286-
it "returns nil" do
287-
expect(mongoized).to eq(7200)
286+
it "returns the casted value as a string" do
287+
expect(mongoized).to eq("7200.0")
288288
end
289289
end
290290

0 commit comments

Comments
 (0)