Skip to content

Commit

Permalink
Merge pull request rails#49779 from arBmind/patch-1
Browse files Browse the repository at this point in the history
Fix (2) case in money.rb
  • Loading branch information
jonathanhefner authored Oct 25, 2023
2 parents a475175 + bcd3589 commit ea4c48a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def cast_value(value)
value = value.sub(/^\((.+)\)$/, '-\1') # (4)
case value
when /^-?\D*+[\d,]+\.\d{2}$/ # (1)
value.gsub!(/[^-\d.]/, "")
value.delete!("^-0-9.")
when /^-?\D*+[\d.]+,\d{2}$/ # (2)
value.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
value.delete!("^-0-9,")
value.tr!(",", ".")
end

super(value)
Expand Down
24 changes: 16 additions & 8 deletions activerecord/test/cases/adapters/postgresql/money_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ def test_money_values

def test_money_type_cast
type = PostgresqlMoney.type_for_attribute("wealth")
assert_equal(12345678.12, type.cast(+"$12,345,678.12"))
assert_equal(12345678.12, type.cast(+"$12.345.678,12"))
assert_equal(12345678.12, type.cast(+"12,345,678.12"))
assert_equal(12345678.12, type.cast(+"12.345.678,12"))
assert_equal(-1.15, type.cast(+"-$1.15"))
assert_equal(-2.25, type.cast(+"($2.25)"))
assert_equal(-1.15, type.cast(+"-1.15"))
assert_equal(-2.25, type.cast(+"(2.25)"))

{
"12,345,678.12" => 12345678.12,
"12.345.678,12" => 12345678.12,
"0.12" => 0.12,
"0,12" => 0.12,
}.each do |string, number|
assert_equal number, type.cast(string)
assert_equal number, type.cast("$#{string}")

assert_equal(-number, type.cast("-#{string}"))
assert_equal(-number, type.cast("-$#{string}"))

assert_equal(-number, type.cast("(#{string})"))
assert_equal(-number, type.cast("($#{string})"))
end
end

def test_money_regex_backtracking
Expand Down

0 comments on commit ea4c48a

Please sign in to comment.