The personal finance app for everyone (by everyone)
Ruby
Format money without resolving number_to_currency options per call (#4690)
Nikolay Kopylov57fbd23ccec6
diff --git a/lib/money/formatting.rb b/lib/money/formatting.rbindex bbaad9ebaa..37e9efd729 100644--- a/lib/money/formatting.rb+++ b/lib/money/formatting.rb@@ -9,11 +9,14 @@ module Money::Formatting# European style: dot as thousands delimiter, comma as decimal separator, symbol before numberEUROPEAN_SYMBOL_BEFORE = %i[nl pt-BR].freeze+ FAST_FORMAT_KEYS = %i[unit precision delimiter separator format].freeze+ DELIMITER_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/+def format(options = {})locale = options[:locale] || I18n.locale- default_opts = format_options(locale)+ opts = format_options(locale).merge(options.except(:locale))- number_to_currency(amount, default_opts.merge(options.except(:locale)))+ fast_format(opts) || number_to_currency(amount, opts)endalias_method :to_s, :format@@ -30,6 +33,37 @@ module Money::Formattingendprivate+ # number_to_currency resolves its options (several I18n lookups and merges) once per+ # converter on every call. For the options built above this gives the same string;+ # nil leaves the rest (significant digits, stripped zeros, custom keys) to it.+ def fast_format(opts)+ precision, format, unit = opts.values_at(:precision, :format, :unit)+ return unless (opts.keys - FAST_FORMAT_KEYS).empty? && precision.is_a?(Integer) && precision >= 0 &&+ format.is_a?(String) && unit.is_a?(String) && amount.finite?++ number_i18n = I18n.translate(:"number.format", default: {})+ currency_i18n = number_i18n.merge(I18n.translate(:"number.currency.format", default: {}))+ return if currency_i18n[:significant] || currency_i18n[:strip_insignificant_zeros]++ precision_i18n = number_i18n.merge(I18n.translate(:"number.precision.format", default: {}))+ return if currency_i18n.key?(:delimiter_pattern) || precision_i18n.key?(:delimiter_pattern)++ round_mode = currency_i18n.fetch(:round_mode) { precision_i18n.fetch(:round_mode, :default) }++ number = amount+ if number.negative?+ number = number.abs+ format = "-#{format}" if number * 10**precision >= 0.5+ end++ rounded = number.round(precision, round_mode.to_sym)+ whole, fraction = rounded.abs.to_s("F").split(".", 2)+ whole = whole.gsub(DELIMITER_REGEX) { "#{$1}#{opts[:delimiter]}" }+ number_s = precision.zero? ? whole : "#{whole}#{opts[:separator]}#{(fraction + "0" * precision)[0, precision]}"++ format.gsub("%n", number_s).gsub("%u", unit)+ end+def get_symbolif currency.symbol == "$" && currency.iso_code != "USD"[ currency.iso_code.first(2), currency.symbol ].joindiff --git a/test/lib/money/formatting_test.rb b/test/lib/money/formatting_test.rbnew file mode 100644index 0000000000..4e1244948f--- /dev/null+++ b/test/lib/money/formatting_test.rb@@ -0,0 +1,48 @@+require "test_helper"++class Money::FormattingTest < ActiveSupport::TestCase+ CURRENCIES = %w[USD EUR JPY KWD CAD MXN BRL GBP INR PLN HUF CHF].freeze+ AMOUNTS = %w[+ 0 0.004 -0.004 0.005 -0.005 0.0049 1 -1 12.345 -12.345 999.995 -999.995+ 1234.5 -1234.5 1000000 -1234567.891 123456789012.346+ ].map { BigDecimal(_1) }.freeze+ OPTIONS = [ { precision: 0 }, { precision: 4 }, { unit: "" }, { format: "%n %u" }, { signify_positive: true } ].freeze++ test "formats exactly like number_to_currency in every locale" do+ I18n.available_locales.each do |locale|+ I18n.with_locale(locale) do+ CURRENCIES.each do |currency|+ AMOUNTS.each { |amount| assert_same_as_number_to_currency Money.new(amount, currency), locale }+ end++ OPTIONS.each do |options|+ [ BigDecimal("-0.005"), BigDecimal("-1234.5") ].each do |amount|+ assert_same_as_number_to_currency Money.new(amount, "EUR"), locale, options+ end+ end+ end+ end+ end++ test "an explicit locale keeps the current locale's number settings" do+ money = Money.new(BigDecimal("-1234.5"), "EUR")++ I18n.with_locale(:en) do+ expected = ActiveSupport::NumberHelper.number_to_currency(money.amount, money.format_options(:de))+ assert_equal expected, money.format(locale: :de)+ end+ end++ test "plain formatting does not go through number_to_currency" do+ money = Money.new(BigDecimal("1234.5"), "USD")+ money.expects(:number_to_currency).never++ assert_equal "$1,234.50", money.format+ end++ private+ def assert_same_as_number_to_currency(money, locale, options = {})+ expected = ActiveSupport::NumberHelper.number_to_currency(money.amount, money.format_options(locale).merge(options))+ assert_equal expected, money.format(options), "#{locale} #{money.currency.iso_code} #{money.amount.to_s("F")} #{options}"+ end+end