mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 13:19:39 +02:00
Add formatting for EUR locales (#1231)
* Add formatting for EUR locales * Fix formatting assertion for EUR in english locales
This commit is contained in:
parent
ab40289eb4
commit
82c298307d
6 changed files with 42 additions and 59 deletions
|
@ -138,13 +138,13 @@ module ApplicationHelper
|
||||||
|
|
||||||
def format_money(number_or_money, options = {})
|
def format_money(number_or_money, options = {})
|
||||||
money = Money.new(number_or_money)
|
money = Money.new(number_or_money)
|
||||||
options.reverse_merge!(money.default_format_options)
|
options.reverse_merge!(money.format_options(I18n.locale))
|
||||||
number_to_currency(money.amount, options)
|
number_to_currency(money.amount, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_money_without_symbol(number_or_money, options = {})
|
def format_money_without_symbol(number_or_money, options = {})
|
||||||
money = Money.new(number_or_money)
|
money = Money.new(number_or_money)
|
||||||
options.reverse_merge!(money.default_format_options)
|
options.reverse_merge!(money.format_options(I18n.locale))
|
||||||
ActiveSupport::NumberHelper.number_to_delimited(money.amount.round(options[:precision] || 0), { delimiter: options[:delimiter], separator: options[:separator] })
|
ActiveSupport::NumberHelper.number_to_delimited(money.amount.round(options[:precision] || 0), { delimiter: options[:delimiter], separator: options[:separator] })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
35
lib/money.rb
35
lib/money.rb
|
@ -1,5 +1,5 @@
|
||||||
class Money
|
class Money
|
||||||
include Comparable, Arithmetic
|
include Comparable, Arithmetic, Formatting
|
||||||
include ActiveModel::Validations
|
include ActiveModel::Validations
|
||||||
|
|
||||||
class ConversionError < StandardError
|
class ConversionError < StandardError
|
||||||
|
@ -54,29 +54,6 @@ class Money
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def cents_str(precision = currency.default_precision)
|
|
||||||
format_str = "%.#{precision}f"
|
|
||||||
amount_str = format_str % amount
|
|
||||||
parts = amount_str.split(currency.separator)
|
|
||||||
|
|
||||||
if parts.length < 2
|
|
||||||
""
|
|
||||||
else
|
|
||||||
parts.last.ljust(precision, "0")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Use `format` for basic formatting only.
|
|
||||||
# Use the Rails number_to_currency helper for more advanced formatting.
|
|
||||||
def format
|
|
||||||
whole_part, fractional_part = sprintf("%.#{currency.default_precision}f", amount).split(".")
|
|
||||||
whole_with_delimiters = whole_part.chars.to_a.reverse.each_slice(3).map(&:join).join(currency.delimiter).reverse
|
|
||||||
formatted_amount = "#{whole_with_delimiters}#{currency.separator}#{fractional_part}"
|
|
||||||
|
|
||||||
currency.default_format.gsub("%n", formatted_amount).gsub("%u", currency.symbol)
|
|
||||||
end
|
|
||||||
alias_method :to_s, :format
|
|
||||||
|
|
||||||
def as_json
|
def as_json
|
||||||
{ amount: amount, currency: currency.iso_code }.as_json
|
{ amount: amount, currency: currency.iso_code }.as_json
|
||||||
end
|
end
|
||||||
|
@ -97,16 +74,6 @@ class Money
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def default_format_options
|
|
||||||
{
|
|
||||||
unit: currency.symbol,
|
|
||||||
precision: currency.default_precision,
|
|
||||||
delimiter: currency.delimiter,
|
|
||||||
separator: currency.separator,
|
|
||||||
format: currency.default_format
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def source_must_be_of_known_type
|
def source_must_be_of_known_type
|
||||||
unless @source.is_a?(Money) || @source.is_a?(Numeric) || @source.is_a?(BigDecimal)
|
unless @source.is_a?(Money) || @source.is_a?(Numeric) || @source.is_a?(BigDecimal)
|
||||||
|
|
35
lib/money/formatting.rb
Normal file
35
lib/money/formatting.rb
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
module Money::Formatting
|
||||||
|
# Fallback formatting. For advanced formatting, use Rails number_to_currency helper.
|
||||||
|
def format
|
||||||
|
whole_part, fractional_part = sprintf("%.#{currency.default_precision}f", amount).split(".")
|
||||||
|
whole_with_delimiters = whole_part.chars.to_a.reverse.each_slice(3).map(&:join).join(currency.delimiter).reverse
|
||||||
|
formatted_amount = "#{whole_with_delimiters}#{currency.separator}#{fractional_part}"
|
||||||
|
|
||||||
|
currency.default_format.gsub("%n", formatted_amount).gsub("%u", currency.symbol)
|
||||||
|
end
|
||||||
|
alias_method :to_s, :format
|
||||||
|
|
||||||
|
def format_options(locale = nil)
|
||||||
|
local_option_overrides = locale_options(locale)
|
||||||
|
|
||||||
|
{
|
||||||
|
unit: currency.symbol,
|
||||||
|
precision: currency.default_precision,
|
||||||
|
delimiter: currency.delimiter,
|
||||||
|
separator: currency.separator,
|
||||||
|
format: currency.default_format
|
||||||
|
}.merge(local_option_overrides)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def locale_options(locale)
|
||||||
|
case [ currency.iso_code, locale.to_sym ]
|
||||||
|
when [ "EUR", :nl ], [ "EUR", :pt ]
|
||||||
|
{ delimiter: ".", separator: ",", format: "%u %n" }
|
||||||
|
when [ "EUR", :en ], [ "EUR", :en_IE ]
|
||||||
|
{ delimiter: ",", separator: "." }
|
||||||
|
else
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -25,9 +25,9 @@ class ApplicationHelperTest < ActionView::TestCase
|
||||||
|
|
||||||
test "#totals_by_currency(collection: collection, money_method: money_method)" do
|
test "#totals_by_currency(collection: collection, money_method: money_method)" do
|
||||||
assert_equal "$3.00", totals_by_currency(collection: [ @account1, @account2 ], money_method: :balance_money)
|
assert_equal "$3.00", totals_by_currency(collection: [ @account1, @account2 ], money_method: :balance_money)
|
||||||
assert_equal "$3.00 | -€7,00", totals_by_currency(collection: [ @account1, @account2, @account3 ], money_method: :balance_money)
|
assert_equal "$3.00 | -€7.00", totals_by_currency(collection: [ @account1, @account2, @account3 ], money_method: :balance_money)
|
||||||
assert_equal "", totals_by_currency(collection: [], money_method: :balance_money)
|
assert_equal "", totals_by_currency(collection: [], money_method: :balance_money)
|
||||||
assert_equal "$0.00", totals_by_currency(collection: [ Account.new(currency: "USD", balance: 0) ], money_method: :balance_money)
|
assert_equal "$0.00", totals_by_currency(collection: [ Account.new(currency: "USD", balance: 0) ], money_method: :balance_money)
|
||||||
assert_equal "-$3.00 | €7,00", totals_by_currency(collection: [ @account1, @account2, @account3 ], money_method: :balance_money, negate: true)
|
assert_equal "-$3.00 | €7.00", totals_by_currency(collection: [ @account1, @account2, @account3 ], money_method: :balance_money, negate: true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,25 +28,6 @@ class Money::CurrencyTest < ActiveSupport::TestCase
|
||||||
assert_equal 2, @currency.default_precision
|
assert_equal 2, @currency.default_precision
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can extract cents string from amount" do
|
|
||||||
value1 = Money.new(100)
|
|
||||||
value2 = Money.new(100.1)
|
|
||||||
value3 = Money.new(100.12)
|
|
||||||
value4 = Money.new(100.123)
|
|
||||||
value5 = Money.new(200, :jpy)
|
|
||||||
|
|
||||||
assert_equal "00", value1.cents_str
|
|
||||||
assert_equal "10", value2.cents_str
|
|
||||||
assert_equal "12", value3.cents_str
|
|
||||||
assert_equal "12", value4.cents_str
|
|
||||||
assert_equal "", value5.cents_str
|
|
||||||
|
|
||||||
assert_equal "", value4.cents_str(0)
|
|
||||||
assert_equal "1", value4.cents_str(1)
|
|
||||||
assert_equal "12", value4.cents_str(2)
|
|
||||||
assert_equal "123", value4.cents_str(3)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "step returns the smallest value of the currency" do
|
test "step returns the smallest value of the currency" do
|
||||||
assert_equal 0.01, @currency.step
|
assert_equal 0.01, @currency.step
|
||||||
end
|
end
|
||||||
|
|
|
@ -85,8 +85,8 @@ class MoneyTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can cast to string with basic formatting" do
|
test "can cast to string with basic formatting" do
|
||||||
assert_equal "$1,000.90", Money.new(1000.899).format
|
assert_equal "$1,000.90", Money.new(1000.899).to_s
|
||||||
assert_equal "€1.000,12", Money.new(1000.12, :eur).format
|
assert_equal "€1.000,12", Money.new(1000.12, :eur).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
test "converts currency when rate available" do
|
test "converts currency when rate available" do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue