`NumberHelper`: handle objects responding `to_d`

This commit is contained in:
fatkodima 2023-10-11 01:38:21 +03:00
parent c8149e6c57
commit fe3b07f683
2 changed files with 14 additions and 1 deletions

View File

@ -179,8 +179,10 @@ module ActiveSupport
case number
when Float, Rational
number.to_d(0)
else
when String
BigDecimal(number, exception: false)
else
number.to_d rescue nil
end
end
end

View File

@ -15,6 +15,16 @@ module ActiveSupport
extend ActiveSupport::NumberHelper
end
class NumberWithToD
def initialize(number)
@number = number
end
def to_d
@number.to_d
end
end
def setup
@instance_with_helpers = TestClassWithInstanceNumberHelpers.new
end
@ -95,6 +105,7 @@ module ActiveSupport
assert_equal("-$,11", number_helper.number_to_currency("-,11"))
assert_equal("$0.00", number_helper.number_to_currency(-0.0))
assert_equal("$0.00", number_helper.number_to_currency("-0.0"))
assert_equal("$1.23", number_helper.number_to_currency(NumberWithToD.new(1.23)))
end
end