Skip to content

Commit

Permalink
Fix #910
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyvince committed Jun 24, 2024
1 parent bddaab0 commit 14ec99f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/helpers/accounts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/views/accounts/_accountable_group.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<p><%= to_accountable_title(Accountable.from_type(group)) %></p>
<span class="text-gray-400 mx-2">&middot;</span>
<p><%= accounts.count %></p>
<p class="ml-auto"><%= format_money accounts.sum(&:balance_money) %></p>
<p class="ml-auto"><%= format_accounts_balance(accounts) %></p>
</div>
<div class="bg-white">
<% accounts.each do |account| %>
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/accounts_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "test_helper"

class AccountsHelperTest < ActionView::TestCase
def setup
@account1 = Account.new(currency: 'USD', balance: 1)

Check failure on line 5 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
@account2 = Account.new(currency: 'USD', balance: 2)

Check failure on line 6 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
@account3 = Account.new(currency: 'EUR', balance: 7)

Check failure on line 7 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end

test "#format_accounts_balance(accounts)" do
assert_equal "$3.00", format_accounts_balance([@account1, @account2])

Check failure on line 11 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 11 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
assert_equal "$3.00, €7,00", format_accounts_balance([@account1, @account2, @account3])

Check failure on line 12 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 12 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
assert_equal "", format_accounts_balance([])
assert_equal "$0.00", format_accounts_balance([Account.new(currency: 'USD', balance: 0)])

Check failure on line 14 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.

Check failure on line 14 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Check failure on line 14 in test/helpers/accounts_helper_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/SpaceInsideArrayLiteralBrackets: Use space inside array brackets.
end
end

0 comments on commit 14ec99f

Please sign in to comment.