Skip to content

remove deprecated formatting rules #1151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
## Upcoming 7.0.0.alpha

- **Breaking change**: Require Ruby >= 3.1 and i18n ~> 1.9
- **Breaking change**: Remove deprecated formatting rules:
- `:html`
- `:html_wrap_symbol`
- `:symbol_position`
- `:symbol_before_without_space`
- `:symbol_after_without_space`
- **Breaking change**: Remove deprecated methods:
- `Money.infinite_precision`.
- `Money.infinite_precision=`.
Expand Down
58 changes: 3 additions & 55 deletions lib/money/money/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,6 @@ class Formatter
# Money.new(10000000, "INR").format(south_asian_number_formatting: true) #=> "1,00,000.00"
# Money.new(10000000).format(south_asian_number_formatting: true) #=> "$1,00,000.00"
#
# @option rules [Boolean, nil] :symbol_before_without_space (true) Whether
# a space between the money symbol and the amount should be inserted when
# +:symbol_position+ is +:before+. The default is true (meaning no space). Ignored
# if +:symbol+ is false or +:symbol_position+ is not +:before+.
#
# @example
# # Default is to not insert a space.
# Money.new(100, "USD").format #=> "$1.00"
#
# # Same thing.
# Money.new(100, "USD").format(symbol_before_without_space: true) #=> "$1.00"
#
# # If set to false, will insert a space.
# Money.new(100, "USD").format(symbol_before_without_space: false) #=> "$ 1.00"
#
# @option rules [Boolean, nil] :symbol_after_without_space (false) Whether
# a space between the amount and the money symbol should be inserted when
# +:symbol_position+ is +:after+. The default is false (meaning space). Ignored
# if +:symbol+ is false or +:symbol_position+ is not +:after+.
#
# @example
# # Default is to insert a space.
# Money.new(100, "USD").format(symbol_position: :after) #=> "1.00 $"
#
# # If set to true, will not insert a space.
# Money.new(100, "USD").format(symbol_position: :after, symbol_after_without_space: true) #=> "1.00$"
#
# @option rules [Boolean, String, nil] :decimal_mark (true) Whether the
# currency should be separated by the specified character or '.'
#
Expand Down Expand Up @@ -140,13 +113,6 @@ class Formatter
# # default to "," as thousands_separator.
# Money.new(100000, "FOO").format #=> "$1,000.00"
#
# @option rules [Boolean] :html (false) Whether the currency should be
# HTML-formatted. Only useful in combination with +:with_currency+.
#
# @example
# Money.ca_dollar(570).format(html: true, with_currency: true)
# #=> "$5.70 <span class=\"currency\">CAD</span>"
#
# @option rules [Boolean] :html_wrap (false) Whether all currency parts should be HTML-formatted.
#
# @example
Expand Down Expand Up @@ -182,20 +148,6 @@ class Formatter
# Money.new(10000, "USD").format(disambiguate: true) #=> "$100.00"
# Money.new(10000, "CAD").format(disambiguate: true) #=> "C$100.00"
#
# @option rules [Boolean] :html_wrap_symbol (false) Wraps the currency symbol
# in a html <span> tag.
#
# @example
# Money.new(10000, "USD").format(html_wrap_symbol: true)
# #=> "<span class=\"currency_symbol\">$100.00</span>
#
# @option rules [Symbol] :symbol_position (:before) `:before` if the currency
# symbol goes before the amount, `:after` if it goes after.
#
# @example
# Money.new(10000, "USD").format(symbol_position: :before) #=> "$100.00"
# Money.new(10000, "USD").format(symbol_position: :after) #=> "100.00 $"
#
# @option rules [Boolean] :translate (true) `true` Checks for custom
# symbol definitions using I18n.
#
Expand Down Expand Up @@ -301,9 +253,7 @@ def append_sign(formatted_number)
symbol_value = symbol_value_from(rules)

if symbol_value && !symbol_value.empty?
if rules[:html_wrap_symbol]
symbol_value = "<span class=\"currency_symbol\">#{symbol_value}</span>"
elsif rules[:html_wrap]
if rules[:html_wrap]
symbol_value = html_wrap(symbol_value, "currency-symbol")
end

Expand All @@ -318,9 +268,7 @@ def append_sign(formatted_number)
def append_currency_symbol(formatted_number)
if rules[:with_currency]
currency_part =
if rules[:html]
"<span class=\"currency\">#{currency}</span>"
elsif rules[:html_wrap]
if rules[:html_wrap]
html_wrap(currency.to_s, "currency")
else
currency.to_s
Expand Down Expand Up @@ -401,7 +349,7 @@ def symbol_value_from(rules)
else
""
end
elsif rules[:html] || rules[:html_wrap]
elsif rules[:html_wrap]
currency.html_entity == '' ? currency.symbol : currency.html_entity
elsif rules[:disambiguate] && currency.disambiguate_symbol
currency.disambiguate_symbol
Expand Down
51 changes: 3 additions & 48 deletions lib/money/money/formatting_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ def initialize(currency, *raw_rules)
@rules = translate_formatting_rules(@rules) if @rules[:translate]
@rules[:format] ||= determine_format_from_formatting_rules(@rules)
@rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)

warn_about_deprecated_rules(@rules)
end

def [](key)
Expand Down Expand Up @@ -71,14 +69,10 @@ def translate_formatting_rules(rules)
end

def determine_format_from_formatting_rules(rules)
return currency.format if currency.format && !rules.has_key?(:symbol_position)

symbol_position = symbol_position_from(rules)

if symbol_position == :before
rules.fetch(:symbol_before_without_space, true) ? '%u%n' : '%u %n'
if currency.format
currency.format
else
rules[:symbol_after_without_space] ? '%n%u' : '%n %u'
currency.symbol_first? ? "%u%n" : "%n %u"
end
end

Expand All @@ -90,44 +84,5 @@ def delimiter_pattern_rule(rules)
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
end
end

def symbol_position_from(rules)
if rules.has_key?(:symbol_position)
if [:before, :after].include?(rules[:symbol_position])
return rules[:symbol_position]
else
raise ArgumentError, ":symbol_position must be ':before' or ':after'"
end
elsif currency.symbol_first?
:before
else
:after
end
end

def warn_about_deprecated_rules(rules)
if rules.has_key?(:symbol_position)
position = rules[:symbol_position]
template = position == :before ? '%u%n' : '%n%u'

warn "[DEPRECATION] `symbol_position: :#{position}` is deprecated - you can replace it with `format: #{template}`"
end

if rules.has_key?(:symbol_before_without_space)
warn "[DEPRECATION] `symbol_before_without_space:` option is deprecated - you can replace it with `format: '%u%n'`"
end

if rules.has_key?(:symbol_after_without_space)
warn "[DEPRECATION] `symbol_after_without_space:` option is deprecated - you can replace it with `format: '%n%u'`"
end

if rules.has_key?(:html)
warn "[DEPRECATION] `html` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency and if you use `with_currency` option, currency element class changes from `currency` to `money-currency`."
end

if rules.has_key?(:html_wrap_symbol)
warn "[DEPRECATION] `html_wrap_symbol` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency."
end
end
end
end
59 changes: 1 addition & 58 deletions sig/lib/money/money/formatter.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,6 @@ class Money
# Money.new(10000000, "INR").format(south_asian_number_formatting: true) #=> "1,00,000.00"
# Money.new(10000000).format(south_asian_number_formatting: true) #=> "$1,00,000.00"
#
# @option rules [Boolean, nil] :symbol_before_without_space (true) Whether
# a space between the money symbol and the amount should be inserted when
# +:symbol_position+ is +:before+. The default is true (meaning no space). Ignored
# if +:symbol+ is false or +:symbol_position+ is not +:before+.
#
# @example
# # Default is to not insert a space.
# Money.new(100, "USD").format #=> "$1.00"
#
# # Same thing.
# Money.new(100, "USD").format(symbol_before_without_space: true) #=> "$1.00"
#
# # If set to false, will insert a space.
# Money.new(100, "USD").format(symbol_before_without_space: false) #=> "$ 1.00"
#
# @option rules [Boolean, nil] :symbol_after_without_space (false) Whether
# a space between the amount and the money symbol should be inserted when
# +:symbol_position+ is +:after+. The default is false (meaning space). Ignored
# if +:symbol+ is false or +:symbol_position+ is not +:after+.
#
# @example
# # Default is to insert a space.
# Money.new(100, "USD").format(symbol_position: :after) #=> "1.00 $"
#
# # If set to true, will not insert a space.
# Money.new(100, "USD").format(symbol_position: :after, symbol_after_without_space: true) #=> "1.00$"
#
# @option rules [Boolean, String, nil] :decimal_mark (true) Whether the
# currency should be separated by the specified character or '.'
#
Expand Down Expand Up @@ -133,28 +106,12 @@ class Money
# # default to "," as thousands_separator.
# Money.new(100000, "FOO").format #=> "$1,000.00"
#
# @option rules [Boolean] :html (false) Whether the currency should be
# HTML-formatted. Only useful in combination with +:with_currency+.
#
# @example
# Money.ca_dollar(570).format(html: true, with_currency: true)
# #=> "$5.70 <span class=\"currency\">CAD</span>"
#
# @option rules [Boolean] :html_wrap (false) Whether all currency parts should be HTML-formatted.
#
# @example
# Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
# #=> "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
#
# @option rules [Boolean] :sign_before_symbol (false) Whether the sign should be
# before the currency symbol.
#
# @example
# # You can specify to display the sign before the symbol for negative numbers
# Money.new(-100, "GBP").format(sign_before_symbol: true) #=> "-£1.00"
# Money.new(-100, "GBP").format(sign_before_symbol: false) #=> "£-1.00"
# Money.new(-100, "GBP").format #=> "£-1.00"
#
# @option rules [Boolean] :sign_positive (false) Whether positive numbers should be
# signed, too.
#
Expand All @@ -175,20 +132,6 @@ class Money
# Money.new(10000, "USD").format(disambiguate: true) #=> "$100.00"
# Money.new(10000, "CAD").format(disambiguate: true) #=> "C$100.00"
#
# @option rules [Boolean] :html_wrap_symbol (false) Wraps the currency symbol
# in a html <span> tag.
#
# @example
# Money.new(10000, "USD").format(disambiguate: false)
# #=> "<span class=\"currency_symbol\">$100.00</span>
#
# @option rules [Symbol] :symbol_position (:before) `:before` if the currency
# symbol goes before the amount, `:after` if it goes after.
#
# @example
# Money.new(10000, "USD").format(symbol_position: :before) #=> "$100.00"
# Money.new(10000, "USD").format(symbol_position: :after) #=> "100.00 $"
#
# @option rules [Boolean] :translate (true) `true` Checks for custom
# symbol definitions using I18n.
#
Expand Down Expand Up @@ -268,4 +211,4 @@ class Money

def symbol_value_from: (Hash[Symbol, untyped] rules) -> (untyped | untyped | "" | untyped)
end
end
end
6 changes: 1 addition & 5 deletions sig/lib/money/money/formatting_rules.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,5 @@ class Money
def determine_format_from_formatting_rules: (Hash[Symbol, untyped] rules) -> Hash[Symbol, untyped]

def delimiter_pattern_rule: (Hash[Symbol, untyped] rules) -> ::Regexp

def symbol_position_from: (Hash[Symbol, untyped] rules) -> (Hash[Symbol, untyped] | :before | :after)

def warn_about_deprecated_rules: (Hash[Symbol, untyped] rules) -> void
end
end
end
18 changes: 0 additions & 18 deletions spec/money/formatting_rules_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,4 @@
expect(rules).to eq(separator: '.')
expect(rules).not_to eq(new_rules)
end

context 'when the position is :before' do
it 'warns about deprecated :symbol_position' do
expect_any_instance_of(Money::FormattingRules).to receive(:warn)
.with('[DEPRECATION] `symbol_position: :before` is deprecated - you can replace it with `format: %u%n`')

Money::FormattingRules.new(Money::Currency.new('USD'), symbol_position: :before)
end
end

context "when the position is :after" do
it 'warns about deprecated :symbol_position' do
expect_any_instance_of(Money::FormattingRules).to receive(:warn)
.with('[DEPRECATION] `symbol_position: :after` is deprecated - you can replace it with `format: %n%u`')

Money::FormattingRules.new(Money::Currency.new('USD'), symbol_position: :after)
end
end
end
Loading