From 14ec99f54cb74a6065ab1b7d3f12fa14c53e6b31 Mon Sep 17 00:00:00 2001 From: Tony Yesudas Date: Mon, 24 Jun 2024 13:02:06 +0200 Subject: [PATCH] Fix #910 --- app/helpers/accounts_helper.rb | 7 +++++++ app/views/accounts/_accountable_group.html.erb | 2 +- test/helpers/accounts_helper_test.rb | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/helpers/accounts_helper_test.rb diff --git a/app/helpers/accounts_helper.rb b/app/helpers/accounts_helper.rb index 54ec09faa61..d1935aefbcc 100644 --- a/app/helpers/accounts_helper.rb +++ b/app/helpers/accounts_helper.rb @@ -37,4 +37,11 @@ def class_mapping(accountable_type) "Vehicle" => { text: "text-pink-500", bg: "bg-pink-500", bg_transparent: "bg-pink-500/10", fill: "fill-pink-500", hex: "#F23E94" } }.fetch(accountable_type, { text: "text-gray-500", bg: "bg-gray-500", bg_transparent: "bg-gray-500/10", fill: "fill-gray-500", hex: "#737373" }) end + + def format_accounts_balance(accounts, options = {}) + accounts.group_by(&:currency) + .transform_values { |acc| acc.sum(&:balance_money) } + .map { |_currency, balance| balance.to_s } + .join(", ") + end end diff --git a/app/views/accounts/_accountable_group.html.erb b/app/views/accounts/_accountable_group.html.erb index 199c8fe1252..b958183df45 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) %>

+

<%= format_accounts_balance(accounts) %>

<% accounts.each do |account| %> diff --git a/test/helpers/accounts_helper_test.rb b/test/helpers/accounts_helper_test.rb new file mode 100644 index 00000000000..be1c90c5818 --- /dev/null +++ b/test/helpers/accounts_helper_test.rb @@ -0,0 +1,16 @@ +require "test_helper" + +class AccountsHelperTest < ActionView::TestCase + 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 "#format_accounts_balance(accounts)" do + assert_equal "$3.00", format_accounts_balance([@account1, @account2]) + assert_equal "$3.00, €7,00", format_accounts_balance([@account1, @account2, @account3]) + assert_equal "", format_accounts_balance([]) + assert_equal "$0.00", format_accounts_balance([Account.new(currency: 'USD', balance: 0)]) + end +end