Skip to content

Commit db1eb4c

Browse files
Correct BSON::Decimal128 handling in normalize_value
Correctly use BSON::Decimal128#to_d if it is available in the driver
1 parent 211d466 commit db1eb4c

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/mongoid/attributes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,12 @@ def lookup_attribute_presence(name, value)
371371
# comparison purposes. This is necessary because `BSON::Decimal128` does
372372
# not implement `#==` in a way that is compatible with `BigDecimal`.
373373
def normalize_value(value)
374-
value.is_a?(BSON::Decimal128) ? BigDecimal(value.to_s) : value
374+
if value.is_a?(BSON::Decimal128)
375+
# BSON::Decimal128#to_d was introduced in driver version 5.0.0
376+
value.respond_to?(:to_d) ? value.to_d : BigDecimal(value.to_s)
377+
else
378+
value
379+
end
375380
end
376381

377382
# Determine if the attribute will not change, by comparing the current

0 commit comments

Comments
 (0)