The personal finance app for everyone
Refactor balance sheet weight calculation and improve group weight rendering
Josh Pigford5cfb4addbdb7
diff --git a/app/models/balance_sheet.rb b/app/models/balance_sheet.rbindex 9a3d43ea..8c2af0f3 100644--- a/app/models/balance_sheet.rb+++ b/app/models/balance_sheet.rb@@ -60,7 +60,7 @@ class BalanceSheetkey = 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 BalanceSheetcolor: 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)++ groupendgroups.sort_by do |group|diff --git a/app/views/pages/dashboard/_balance_sheet.html.erb b/app/views/pages/dashboard/_balance_sheet.html.erbindex 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 %><div class="ml-auto flex items-center text-right gap-6"><div class="w-28 shrink-0 flex items-center justify-end gap-2">- <%= 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 %></div><div class="w-40 shrink-0">diff --git a/app/views/pages/dashboard/_group_weight.html.erb b/app/views/pages/dashboard/_group_weight.html.erbindex 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 %>+<div class="w-full flex items-center justify-between gap-2"><div class="flex gap-[3px]"><% 10.times do |i| %>- <div class="w-0.5 h-2.5 rounded-lg <%= i < (weight / 10.0).ceil ? "" : "opacity-20" %>" style="background-color: <%= color %>;"></div>+ <div class="w-0.5 h-2.5 rounded-lg <%= i < (effective_weight / 10.0).ceil ? "" : "opacity-20" %>" style="background-color: <%= color %>;"></div><% end %></div>- <p class="text-sm"><%= number_to_percentage(weight, precision: 2) %></p>+ <p class="text-sm"><%= number_to_percentage(effective_weight, precision: 2) %></p></div>