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