From 7ec30400e12d547ab573281c442ccb8a48d80495 Mon Sep 17 00:00:00 2001 From: Kir Shatrov Date: Sun, 22 Jan 2017 10:28:01 -0500 Subject: [PATCH] Make BigDecimal casting consistent on different platforms Right now it behaves differently on JRuby: ``` --- expected +++ actual @@ -1 +1 @@ -# +# ``` My initial PR (https://github.com/rails/rails/pull/27324) offered to let the precision to be decided by the platform and change the test expection, but other contributors suggested that we should change the default precision in Rails to be consistent of all platforms. The value (18) comes from the max default precision that comes from casting Rational(1/3) to BigDecimal. --- activemodel/lib/active_model/type/decimal.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/activemodel/lib/active_model/type/decimal.rb b/activemodel/lib/active_model/type/decimal.rb index 6c5c0451c6..541a12c8a1 100644 --- a/activemodel/lib/active_model/type/decimal.rb +++ b/activemodel/lib/active_model/type/decimal.rb @@ -4,6 +4,7 @@ module ActiveModel module Type class Decimal < Value # :nodoc: include Helpers::Numeric + BIGDECIMAL_PRECISION = 18 def type :decimal @@ -21,7 +22,7 @@ def cast_value(value) when ::Float convert_float_to_big_decimal(value) when ::Numeric, ::String - BigDecimal(value, precision.to_i) + BigDecimal(value, precision || BIGDECIMAL_PRECISION) else if value.respond_to?(:to_d) value.to_d