aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/type/decimal.rb10
-rw-r--r--activerecord/test/cases/types_test.rb2
2 files changed, 7 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/type/decimal.rb b/activerecord/lib/active_record/type/decimal.rb
index 33d4e37ea3..1c7edc12f5 100644
--- a/activerecord/lib/active_record/type/decimal.rb
+++ b/activerecord/lib/active_record/type/decimal.rb
@@ -14,14 +14,16 @@ module ActiveRecord
private
def cast_value(value)
- if value.class == Rational
- BigDecimal.new(value, precision.to_i)
- elsif value.respond_to?(:to_d)
+ case value
+ when Numeric, String, Rational
+ BigDecimal(value, precision.to_i)
+ when proc { value.respond_to?(:to_d) }
value.to_d
else
- value.to_s.to_d
+ cast_value(value.to_s)
end
end
+
end
end
end
diff --git a/activerecord/test/cases/types_test.rb b/activerecord/test/cases/types_test.rb
index 4ba8a3e09f..541c0a4e12 100644
--- a/activerecord/test/cases/types_test.rb
+++ b/activerecord/test/cases/types_test.rb
@@ -103,7 +103,7 @@ module ActiveRecord
end
def test_type_cast_rational_to_decimal_with_precision
- type = Type::Decimal.new(:precision => 2)
+ type = Type::Decimal.new(precision: 2)
assert_equal BigDecimal("0.33"), type.type_cast_from_user(Rational(1, 3))
end