Show the investment card when the period has a disposal
Closes #4407
Fork pre-flight: jaysbeekay/sure#536, from the same branch.
Problem
ReportsController#build_investment_metrics decides whether to show the whole Investment Performance card with one check:
return { has_investments: false } unless investment_accounts.any?
InvestmentStatement#investment_accounts covers visible accounts only, which means draft and active. The card's realised-gains half (build_gains_by_tax_treatment) lists the period's sell trades from every account in Account.included_in_reports. That scope has no status filter, so disabled accounts are included.
So when a family's only investment account is closed, the card is not rendered at all, even though it would list a disposal for the period.
Change
One file, app/controllers/reports_controller.rb:
- A shared query. The card's sell-trade query (the period, included in reports, negative quantity, transfers out excluded) moves out of
build_gains_by_tax_treatmentinto a privateperiod_sell_tradesmethod.build_gains_by_tax_treatmentadds itsincludesto it and loads it as before, so the card's figures are unchanged. - The gate also opens on a disposal. It is now
investment_accounts.any? || period_disposals_in_finances?. That second check asks the sameperiod_sell_tradesrelation, so the gate and the realised figures agree on what counts as a disposal. It is also limited to accounts the user includes in their finances, which is the per-user scopeinvestment_accountsalready applies. Another member's account therefore does not open the card for this user. The check is oneEXISTSquery, and it runs only wheninvestment_accountsis empty.
This answers "is there anything to show" from upstream's own data. No new model.
Blast radius. Who reads has_investments, and what changes for each:
_investment_performance.html.erband the section list'svisible:. The card now renders for a closed-account family that has a disposal in the period. Every part of the card that reads a collection is already guarded:top_holdings.any?,accounts.any?,gains_by_tax_treatment.present?, and the trends are nil-checked. The card shows the summary row (portfolio value $0, the true figure) and the realised section with the disposal.- The
investment_flowssection'svisible:ishas_investments && (contributions > 0 || withdrawals > 0). Its flows come fromInvestmentFlowStatement, which is unchanged, so it shows only when there are flows, as before. print.html.erb(@investment_metrics[:has_investments]). The printed report now also shows its investment summary for such a family. That summary has no realised section, so it prints the true zeros: portfolio value, contributions and withdrawals over visible accounts. This is a visible change, called out here.- The gate is now period-dependent. The same family can see the card for a period that has a disposal and not for one that has none.
Siblings.
pages_controller.rb#investment_summary_available?andpages/dashboard/_investment_summary.html.erbalso gate oninvestment_accounts.any?. They show no realised gains, so they have nothing extra to show, and they are deliberately unchanged.- The other
qty < 0readers (InvestmentStatement::Totals,Portfolio::FlowClassifier) measure flows, not this card's disposals. They are unchanged.
Deliberately left out. The realised-gains half reads Current.family.trades without the per-user included_in_finances_for scope. A user who has an investment account in scope therefore already sees other members' disposals on this card. That is pre-existing and separate, and this change does not widen it: the gate's disposal check is per-user. It needs its own issue.
Tests
test/controllers/reports_controller_test.rb, 6 new tests:
- The defect. A family whose only investment account is disabled, with a disposal in the period, gets the card, and the disposal line is on it at $100.00.
- Guards:
- with no investment accounts and no disposals, there is no card;
- with a closed account whose disposal falls outside the period, there is no card;
- a transfer out of a closed account does not open the card;
- another member's closed account, with a disposal this user does not include in their finances, does not open the card;
- an open investment account still shows the card in a period with no disposals.
Fail-first. Against upstream main, before the fix, the defect test failed for the expected reason:
the card must render for a family whose only investment account is closed.
Expected: 1
Actual: 0
The guard tests pass both before and after the fix. Each one exists to catch a specific wrong version of the fix (see the table below).
Mutations. Each part of the fix was broken in turn, and the test file was run against each break:
| Mutation | Caught by |
|---|---|
Gate reverted to investment_accounts.any? | the defect test |
| Gate's disposal check without the per-user scope | another member's closed account does not show the card |
Gate checks disposals only (drops investment_accounts.any?) | an open investment account still shows the card in a period with no disposals (added after this mutation first survived) |
| Gate's disposal check ignores the period and the transfer exclusion | the outside-the-period test; the transfer-out test |
| Gate's disposal check ignores only the transfer exclusion | the transfer-out test |
All 5 were caught.
Validation
bin/rails test: 12652 runs, 58468 assertions, 0 failures, 0 errors, 36 skips.bin/rubocopon the changed files: no offenses.bin/brakeman --no-pager: 0 warnings.- No ERB, JavaScript or CSS changed. The rendered card changes for the affected families. Screenshots: below.
- Checked against
AGENTS.md:- the controller change stays small;
- tests use Minitest and fixtures;
Current.userandCurrent.familyare used;- the per-user account scope is preserved;
- commit subjects are imperative and at most 72 characters;
- no new dependency or migration.
Screenshots
Captured by a system test against the test fixtures: a family whose only investment account is closed, with a sale this month at a $100.00 gain. On main at 3ae36a699 the Reports page renders no Investment Performance section at all. On this branch it renders, and lists the gain:

Since review
e2784b5f2:period_sell_tradesis now scoped to the accounts the user includes in their finances, and leaves out excluded entries. Both the gate and the realised-gains list read it. This also closes a leak already onmain: with an open investment account, the list was family-wide and could show another member's unshared disposals. Full suite: 12,657 runs, 0 failures.