Files
sure/app/models/account/chartable.rb
Antoine 🚀🏃‍♂️ 6b6ae0ca31 feat(accounts): add Gains / ROI chart view for investment accounts (#2660)
* feat(accounts): add Gains / ROI chart view for investment accounts

Adds a fourth chart view to the account details page showing the
historical unrealized gains series (market value - cost basis per
holding, summed daily with LOCF and FX conversion), following the
same ChartSeriesBuilder/Series pipeline as the existing views.

Holdings without a usable cost basis (nil, or unlocked zero from
providers) contribute zero gain, matching Holding#avg_cost semantics.

* fix(accounts): carry cost basis forward over gap-filled holdings in gains series

Gap-filled holding rows (weekends, price-history gaps) are persisted
without cost_basis even though the position and basis are unchanged,
which zeroed the gains series on those dates. Look up the basis from
the latest snapshot that has a usable one instead of reading it from
the current row, so already-persisted gap-filled rows are handled too.

* docs(accounts): add method docstrings for gains chart view code

Satisfies the pre-merge docstring coverage check on methods added or
touched by the Gains / ROI feature.

* fix(accounts): sign converted amount like main indicator in gains view

Extracts the gains sign-prefix logic into a shared signed_format helper
so the family-currency converted amount shown on foreign-currency
accounts matches the main indicator (+€79.53 / +$85.00), and adds
component tests covering the positive, negative, non-gains and
foreign-currency formatting paths.

* test(accounts): cover FX conversion path in gains series

The gains_series tests only used USD holdings against a USD target,
leaving the exchange_rates LATERAL join untested. Adds a case with EUR
holdings converted to USD, including LOCF rate carry-forward.

---------

Co-authored-by: Antoine GUYON <agy@ibanfirst.com>
2026-07-25 20:14:56 +02:00

42 lines
1.3 KiB
Ruby

module Account::Chartable
extend ActiveSupport::Concern
SPARKLINE_CACHE_VERSION = "v4"
def favorable_direction
classification == "asset" ? "up" : "down"
end
# Returns the chart Series for this account over the given period.
# Supported views: :balance, :cash_balance, :holdings_balance, :gains.
def balance_series(period: Period.last_30_days, view: :balance, interval: nil)
raise ArgumentError, "Invalid view type" unless [ :balance, :cash_balance, :holdings_balance, :gains ].include?(view.to_sym)
@balance_series ||= {}
memo_key = [ period.start_date, period.end_date, interval ].compact.join("_")
builder = (@balance_series[memo_key] ||= Balance::ChartSeriesBuilder.new(
account_ids: [ id ],
currency: self.currency,
period: period,
favorable_direction: favorable_direction,
interval: interval
))
normalize_linked_investment_series(builder.send("#{view}_series"))
end
def sparkline_series
cache_key = family.build_cache_key("#{id}_sparkline_#{SPARKLINE_CACHE_VERSION}", invalidate_on_data_updates: true)
Rails.cache.fetch(cache_key, expires_in: 24.hours) do
balance_series
end
end
private
def normalize_linked_investment_series(series)
Balance::LinkedInvestmentSeriesNormalizer.new(account: self, series: series).normalize
end
end