From 5cfb4addbdb7d0ce6637e4fbe82ebaefc029f511 Mon Sep 17 00:00:00 2001 From: Josh Pigford Date: Fri, 23 May 2025 12:25:18 -0500 Subject: [PATCH] Refactor balance sheet weight calculation and improve group weight rendering - Update BalanceSheet model to directly calculate account weights based on converted balances. - Modify dashboard view to compute account weight as a percentage of classification total, enhancing clarity. - Adjust group weight partial to handle effective weight, ensuring accurate rendering of weight representation. --- app/models/balance_sheet.rb | 10 ++++------ app/views/pages/dashboard/_balance_sheet.html.erb | 7 ++++++- app/views/pages/dashboard/_group_weight.html.erb | 6 ++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/models/balance_sheet.rb b/app/models/balance_sheet.rb index 9a3d43ea..8c2af0f3 100644 --- a/app/models/balance_sheet.rb +++ b/app/models/balance_sheet.rb @@ -60,7 +60,7 @@ class BalanceSheet key = accountable.model_name.param_key - AccountGroup.new( + group = AccountGroup.new( id: classification ? "#{classification}_#{key}_group" : "#{key}_group", key: key, name: accountable.display_name, @@ -72,13 +72,11 @@ class BalanceSheet color: accountable.color, syncing?: accounts.any?(&:is_syncing), accounts: accounts.map do |account| - account.define_singleton_method(:weight) do - classification_total.zero? ? 0 : account.converted_balance / classification_total.to_d * 100 - end - account - end.sort_by(&:weight).reverse + end.sort_by(&:converted_balance).reverse ) + + group end groups.sort_by do |group| diff --git a/app/views/pages/dashboard/_balance_sheet.html.erb b/app/views/pages/dashboard/_balance_sheet.html.erb index 7a71afe7..337e5268 100644 --- a/app/views/pages/dashboard/_balance_sheet.html.erb +++ b/app/views/pages/dashboard/_balance_sheet.html.erb @@ -116,7 +116,12 @@ <% else %>
- <%= render "pages/dashboard/group_weight", weight: account.weight, color: account_group.color %> + <% + # Calculate weight as percentage of classification total + classification_total = classification_group.total_money.amount + account_weight = classification_total.zero? ? 0 : account.converted_balance / classification_total * 100 + %> + <%= render "pages/dashboard/group_weight", weight: account_weight, color: account_group.color %>
diff --git a/app/views/pages/dashboard/_group_weight.html.erb b/app/views/pages/dashboard/_group_weight.html.erb index d0a8fe0d..c00655a2 100644 --- a/app/views/pages/dashboard/_group_weight.html.erb +++ b/app/views/pages/dashboard/_group_weight.html.erb @@ -1,10 +1,12 @@ <%# locals: (weight:, color:) %> +<% effective_weight = weight.presence || 0 %> +
<% 10.times do |i| %> -
" style="background-color: <%= color %>;">
+
" style="background-color: <%= color %>;">
<% end %>
-

<%= number_to_percentage(weight, precision: 2) %>

+

<%= number_to_percentage(effective_weight, precision: 2) %>