Gate the investment card on having something to show
Closes #206. Option 2, your decision of 2026-09-21.
The defect
ReportsController#build_investment_metrics gates the whole investment card on one scope:
return { has_investments: false } unless investment_accounts.any?
InvestmentStatement#investment_accounts is family.accounts.visible.included_in_reports, and visible is draft and active only. The figures inside that card have never had a status filter — the realised half is measured over included_in_reports, which carries disabled accounts.
So a family whose only investment account is closed is shown nothing, while the card it was denied had a real realised figure for the period. Pre-existing, and not #205's doing: the same fixture renders nothing against both controllers.
The fix
The gate asks a second question — are there disposals in the period — over the same account scope the card's own figures use.
It asks Portfolio::RealizedGains, not a second hand-rolled trades query. Two implementations of one question is the shape of #167, and a gate sitting next to the code that computes the figure is exactly where it would come back.
#any_disposals? is deliberately cheaper than measuring: it loads the period's sell trades and stops, where the figures cost a gain per trade against preloaded holdings and cost bases. That matters because the gate now runs for families with no investments at all, who previously returned on the accounts check alone.
What this costs, stated rather than buried
- One trades query for a family with no investments, on a page everyone loads.
- The gate becomes period-dependent where it was not: the same family can see the card in one period and not the next.
Both are inherent to option 2 and are recorded on the issue as its price. Option 3 would have avoided them by narrowing the figures instead — at the cost of taking a disposal away from families who can see it today.
No view changes, checked rather than assumed
top_holdings (:79) and accounts (:225) are already guarded with .any?, and the trend cards fall back to no_data. A closed-broker card shows the summary row and the realised section, not a shell. The £0 portfolio value stays — it is the true figure, and under this option it only ever appears beside the disposals that explain it. That co-occurrence is precisely why this beats gating on the historical scope, which would have printed the same zero with nothing next to it to make sense of it.
Tests
| Assertion | Mutation that breaks it |
|---|---|
| A family whose only investment account is disabled still gets the card when the period has a disposal | gate reverts to visible accounts only → Expected [] to not be empty |
| A family with no investment accounts and no disposals still gets no card | gate drops the accounts check → existing tests fail: a family holding investments that disposed of nothing loses its card |
The first test asserts on the disposal line, so it proves both that the card rendered and that the figure which earned it a place is on it.
#205 has landed, and the two instances are now one
Both touched build_investment_metrics: #205 gave the card a shared Portfolio::RealizedGains, this gave the gate its question, and each built its own instance. #205 merged first, and the conflict was resolved here at e03e3cd9 by building one instance and asking it twice — any_disposals? for the gate, the disposals themselves for the card. The sell trades are therefore loaded once rather than by each in turn, which is the double-instantiation cubic and Codacy both raised on this PR and which was deferred until this merge could actually make it true.
Basing on main rather than stacking on #205 is what made that possible on schedule: CodeRabbit skips a PR whose base is not the default branch, so a stacked PR would have had no review to resolve the conflict against.
Verification
Full suite 9991 runs, 42435 assertions, 0 failures, 0 errors, 33 skips at the current head; bin/rubocop clean.
Summary by CodeRabbit
- Bug Fixes
- Investment performance cards now appear when the selected period includes investment disposals, even if related accounts are disabled or hidden.
- Realized gains continue to display correctly for disposals from excluded investment accounts.
- Investment performance cards remain hidden when there are no visible investment accounts and no disposals.