From 3baaf8453816e0b6d99a18489fc3b6e899d4eb65 Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Fri, 18 Jul 2025 09:14:26 -0400 Subject: [PATCH] Remove trend calculator --- app/models/account/activity_feed_data.rb | 8 +++++- app/models/balance/trend_calculator.rb | 30 -------------------- app/views/accounts/show/_activity.html.erb | 4 +-- app/views/trades/_trade.html.erb | 10 ------- app/views/transactions/_transaction.html.erb | 17 ++--------- 5 files changed, 11 insertions(+), 58 deletions(-) delete mode 100644 app/models/balance/trend_calculator.rb diff --git a/app/models/account/activity_feed_data.rb b/app/models/account/activity_feed_data.rb index d6e5b14c..4b2de075 100644 --- a/app/models/account/activity_feed_data.rb +++ b/app/models/account/activity_feed_data.rb @@ -6,11 +6,17 @@ class Account::ActivityFeedData def initialize(account, entries) @account = account - @entries = entries + @entries = entries.to_a end # We read balances so we can show "start of day" -> "end of day" balances for each entry date group in the feed def balances + @balances ||= begin + min_date = entries.min_by(&:date).date.prev_day + max_date = entries.max_by(&:date).date + + account.balances.where(date: min_date..max_date, currency: account.currency).order(:date) + end end diff --git a/app/models/balance/trend_calculator.rb b/app/models/balance/trend_calculator.rb deleted file mode 100644 index 990a8339..00000000 --- a/app/models/balance/trend_calculator.rb +++ /dev/null @@ -1,30 +0,0 @@ -# The current system calculates a single, end-of-day balance every day for each account for simplicity. -# In most cases, this is sufficient. However, for the "Activity View", we need to show intraday balances -# to show users how each entry affects their balances. This class calculates intraday balances by -# interpolating between end-of-day balances. -class Balance::TrendCalculator - BalanceTrend = Struct.new(:trend, :cash, keyword_init: true) - - def initialize(balances) - @balances = balances - end - - def trend_for(date) - balance = @balances.find { |b| b.date == date } - prior_balance = @balances.find { |b| b.date == date - 1.day } - - return BalanceTrend.new(trend: nil) unless balance.present? - - BalanceTrend.new( - trend: Trend.new( - current: Money.new(balance.balance, balance.currency), - previous: prior_balance.present? ? Money.new(prior_balance.balance, balance.currency) : nil, - favorable_direction: balance.account.favorable_direction - ), - cash: Money.new(balance.cash_balance, balance.currency), - ) - end - - private - attr_reader :balances -end diff --git a/app/views/accounts/show/_activity.html.erb b/app/views/accounts/show/_activity.html.erb index 3c454f2d..69875439 100644 --- a/app/views/accounts/show/_activity.html.erb +++ b/app/views/accounts/show/_activity.html.erb @@ -76,11 +76,9 @@
- <% calculator = Balance::TrendCalculator.new(@account.balances) %> - <%= entries_by_date(@entries) do |entries| %> <% entries.each_with_index do |entry, index| %> - <%= render entry, balance_trend: index == 0 ? calculator.trend_for(entry.date) : nil, view_ctx: "account" %> + <%= render entry, view_ctx: "account" %> <% end %> <% end %>
diff --git a/app/views/trades/_trade.html.erb b/app/views/trades/_trade.html.erb index 7d940aa6..4e088e9c 100644 --- a/app/views/trades/_trade.html.erb +++ b/app/views/trades/_trade.html.erb @@ -38,16 +38,6 @@ format_money(-entry.amount_money), class: ["text-green-600": entry.amount.negative?] %>
- -
- <% if balance_trend&.trend %> -
- <%= tag.p format_money(balance_trend.trend.current), class: "font-medium text-sm text-primary" %> -
- <% else %> - <%= tag.p "--", class: "font-medium text-sm text-gray-400" %> - <% end %> -
<% end %> <% end %> diff --git a/app/views/transactions/_transaction.html.erb b/app/views/transactions/_transaction.html.erb index b603e305..0abb1b28 100644 --- a/app/views/transactions/_transaction.html.erb +++ b/app/views/transactions/_transaction.html.erb @@ -6,7 +6,7 @@ <%= turbo_frame_tag dom_id(transaction) do %>
"> -
"> +
<%= check_box_tag dom_id(entry, "selection"), disabled: transaction.transfer.present?, class: "checkbox checkbox--light", @@ -93,22 +93,11 @@ <%= render "transactions/transaction_category", transaction: transaction %>
-
+
<%= content_tag :p, transaction.transfer? && view_ctx == "global" ? "+/- #{format_money(entry.amount_money.abs)}" : format_money(-entry.amount_money), class: ["text-green-600": entry.amount.negative?] %> -
- - <% if view_ctx != "global" %> - - <% end %> +
<% end %> <% end %>