Skip to content

Commit ea4c48a

Browse files
Merge pull request rails#49779 from arBmind/patch-1
Fix (2) case in money.rb
2 parents a475175 + bcd3589 commit ea4c48a

File tree

2 files changed

+19
-10
lines changed
  • activerecord

2 files changed

+19
-10
lines changed

activerecord/lib/active_record/connection_adapters/postgresql/oid/money.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ def cast_value(value)
2727
value = value.sub(/^\((.+)\)$/, '-\1') # (4)
2828
case value
2929
when /^-?\D*+[\d,]+\.\d{2}$/ # (1)
30-
value.gsub!(/[^-\d.]/, "")
30+
value.delete!("^-0-9.")
3131
when /^-?\D*+[\d.]+,\d{2}$/ # (2)
32-
value.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
32+
value.delete!("^-0-9,")
33+
value.tr!(",", ".")
3334
end
3435

3536
super(value)

activerecord/test/cases/adapters/postgresql/money_test.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,22 @@ def test_money_values
5454

5555
def test_money_type_cast
5656
type = PostgresqlMoney.type_for_attribute("wealth")
57-
assert_equal(12345678.12, type.cast(+"$12,345,678.12"))
58-
assert_equal(12345678.12, type.cast(+"$12.345.678,12"))
59-
assert_equal(12345678.12, type.cast(+"12,345,678.12"))
60-
assert_equal(12345678.12, type.cast(+"12.345.678,12"))
61-
assert_equal(-1.15, type.cast(+"-$1.15"))
62-
assert_equal(-2.25, type.cast(+"($2.25)"))
63-
assert_equal(-1.15, type.cast(+"-1.15"))
64-
assert_equal(-2.25, type.cast(+"(2.25)"))
57+
58+
{
59+
"12,345,678.12" => 12345678.12,
60+
"12.345.678,12" => 12345678.12,
61+
"0.12" => 0.12,
62+
"0,12" => 0.12,
63+
}.each do |string, number|
64+
assert_equal number, type.cast(string)
65+
assert_equal number, type.cast("$#{string}")
66+
67+
assert_equal(-number, type.cast("-#{string}"))
68+
assert_equal(-number, type.cast("-$#{string}"))
69+
70+
assert_equal(-number, type.cast("(#{string})"))
71+
assert_equal(-number, type.cast("($#{string})"))
72+
end
6573
end
6674

6775
def test_money_regex_backtracking

0 commit comments

Comments
 (0)