1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 13:19:39 +02:00
* Fix #910

* Unify helper for balance formatting in transactions and accounts views

* Remove obsolete method

* Rename helper method format_amount_by_curreny => totals_by_currency
This commit is contained in:
Tony Vincent 2024-06-24 16:56:44 +02:00 committed by GitHub
parent ee53546c1b
commit bbcd3881db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 7 deletions

View file

@ -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

View file

@ -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"><%= totals_by_currency(collection: accounts, money_method: :balance_money) %></p>
</div>
<div class="bg-white">
<% accounts.each do |account| %>

View file

@ -11,13 +11,8 @@
</div>
<div>
<% 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) %>
</div>
</div>
<div class="bg-white shadow-xs rounded-md border border-alpha-black-25 divide-y divide-alpha-black-50">
<%= render group[:transactions] %>

View file

@ -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