Skip to content

Commit

Permalink
Don't ignore disambiguate option when symbol: true
Browse files Browse the repository at this point in the history
Currently, calling `format` with both disambiguate and symbol options
set to true will just return the ambiguous symbol:
```
Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)
=> "$1,999.98"
```

This PR changes the behavior so disambiguate is not ignored if symbol is
true:
```
Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)
=> "US$1,999.98"
```
  • Loading branch information
project-eutopia committed Oct 28, 2016
1 parent aa35ebd commit f401e30
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- `Money#==` will now raise error for non-zero numeric values
- Fixed divmod
- Added disambiguation symbol to USD Dollar
- Use disambiguation symbol when both disambiguate and symbol are true in `format` method

## 6.7.0
- Changed `Money#<=>` to return `nil` if the comparison is inappropriate. (#584)
Expand Down
8 changes: 6 additions & 2 deletions lib/money/money/formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,19 @@ def localize_formatting_rules(rules)
def symbol_value_from(rules)
if rules.has_key?(:symbol)
if rules[:symbol] === true
symbol
if rules[:disambiguate] && currency.disambiguate_symbol
currency.disambiguate_symbol
else
symbol
end
elsif rules[:symbol]
rules[:symbol]
else
""
end
elsif rules[:html]
currency.html_entity == '' ? currency.symbol : currency.html_entity
elsif rules[:disambiguate] and currency.disambiguate_symbol
elsif rules[:disambiguate] && currency.disambiguate_symbol
currency.disambiguate_symbol
else
symbol
Expand Down
16 changes: 16 additions & 0 deletions spec/money/formatting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,22 @@
expect(Money.new(1999_98, "SEK").format(disambiguate: true)).to eq("1 999,98 SEK")
end

it "returns disambiguate signs when disambiguate: true and symbol: true" do
expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)).to eq("US$1,999.98")
expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: true)).to eq("C$1,999.98")
expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: true)).to eq("1.999,98 DKK")
expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: true)).to eq("1.999,98 NOK")
expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: true)).to eq("1 999,98 SEK")
end

it "returns no signs when disambiguate: true and symbol: false" do
expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: false)).to eq("1,999.98")
expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: false)).to eq("1,999.98")
expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: false)).to eq("1.999,98")
expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: false)).to eq("1.999,98")
expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: false)).to eq("1 999,98")
end

it "should never return an ambiguous format with disambiguate: true" do
formatted_results = {}

Expand Down

0 comments on commit f401e30

Please sign in to comment.