diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 67082b18..a5e3af94 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -122,4 +122,11 @@ module ApplicationHelper options.reverse_merge!(money.default_format_options) ActiveSupport::NumberHelper.number_to_delimited(money.amount.round(options[:precision] || 0), { delimiter: options[:delimiter], separator: options[:separator] }) end + + def totals_by_currency(collection:, money_method:, separator: " | ", negate: false, options: {}) + collection.group_by(&:currency) + .transform_values { |item| negate ? item.sum(&money_method) * -1 : item.sum(&money_method) } + .map { |_currency, money| format_money(money) } + .join(separator) + end end diff --git a/app/views/accounts/_accountable_group.html.erb b/app/views/accounts/_accountable_group.html.erb index 199c8fe1..bef3069e 100644 --- a/app/views/accounts/_accountable_group.html.erb +++ b/app/views/accounts/_accountable_group.html.erb @@ -6,7 +6,7 @@

<%= to_accountable_title(Accountable.from_type(group)) %>

·

<%= accounts.count %>

-

<%= format_money accounts.sum(&:balance_money) %>

+

<%= totals_by_currency(collection: accounts, money_method: :balance_money) %>

<% accounts.each do |account| %> diff --git a/app/views/transactions/_date_group.html.erb b/app/views/transactions/_date_group.html.erb index 80de9bd9..a464cfc9 100644 --- a/app/views/transactions/_date_group.html.erb +++ b/app/views/transactions/_date_group.html.erb @@ -11,13 +11,8 @@
- <% transactions_by_currency = group[:transactions].group_by(&:currency) %> - <% transactions_by_currency.each_with_index do |(_currency, transactions), idx| %> - <%= tag.span format_money(-transactions.sum(&:amount_money)) %> - <%= tag.span "|", class: "mx-2" if idx < transactions_by_currency.count - 1 %> - <% end %> + <%= totals_by_currency(collection: group[:transactions], money_method: :amount_money, negate: true) %>
-
<%= render group[:transactions] %> diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb index 14fafe8e..d25531aa 100644 --- a/test/helpers/application_helper_test.rb +++ b/test/helpers/application_helper_test.rb @@ -16,4 +16,18 @@ class ApplicationHelperTest < ActionView::TestCase assert_equal "user", permitted_accountable_partial("User") assert_equal "admin_user", permitted_accountable_partial("AdminUser") end + + def setup + @account1 = Account.new(currency: "USD", balance: 1) + @account2 = Account.new(currency: "USD", balance: 2) + @account3 = Account.new(currency: "EUR", balance: -7) + end + + 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 | -€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 "$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) + end end