1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix case statement to use ::Numeric and ::String

This commit is contained in:
Mariano Valles 2014-07-16 16:23:54 +02:00
parent 7098c9083c
commit 431b4b4d88
3 changed files with 25 additions and 18 deletions

View file

@ -15,7 +15,7 @@ module ActiveRecord
def cast_value(value)
case value
when Numeric, String, Rational
when ::Numeric, ::String
BigDecimal(value, precision.to_i)
when proc { value.respond_to?(:to_d) }
value.to_d

View file

@ -0,0 +1,24 @@
require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class DecimalTest < ActiveRecord::TestCase
def test_type_cast_decimal
type = Type::Decimal.new
assert_equal BigDecimal.new("0"), type.type_cast_from_user(BigDecimal.new("0"))
assert_equal BigDecimal.new("123"), type.type_cast_from_user(123.0)
assert_equal BigDecimal.new("1"), type.type_cast_from_user(:"1")
end
def test_type_cast_rational_to_decimal_with_precision
type = Type::Decimal.new(precision: 2)
assert_equal BigDecimal("0.33"), type.type_cast_from_user(Rational(1, 3))
end
def test_type_cast_rational_to_decimal_without_precision_defaults_to_18_36
type = Type::Decimal.new
assert_equal BigDecimal("0.333333333333333333E0"), type.type_cast_from_user(Rational(1, 3))
end
end
end
end

View file

@ -95,23 +95,6 @@ module ActiveRecord
assert_not type.changed?(nil, nil, nil)
end
def test_type_cast_decimal
type = Type::Decimal.new
assert_equal BigDecimal.new("0"), type.type_cast_from_user(BigDecimal.new("0"))
assert_equal BigDecimal.new("123"), type.type_cast_from_user(123.0)
assert_equal BigDecimal.new("1"), type.type_cast_from_user(:"1")
end
def test_type_cast_rational_to_decimal_with_precision
type = Type::Decimal.new(precision: 2)
assert_equal BigDecimal("0.33"), type.type_cast_from_user(Rational(1, 3))
end
def test_type_cast_rational_to_decimal_without_precision_defaults_to_18_36
type = Type::Decimal.new
assert_equal BigDecimal("0.333333333333333333E0"), type.type_cast_from_user(Rational(1, 3))
end
def test_type_cast_binary
type = Type::Binary.new
assert_equal nil, type.type_cast_from_user(nil)