Show the investment card when the period has a disposal
Part of jaysbeekay/sure#206 (fixed on fork main by jaysbeekay/sure#210, with a fork-only model; this port is standalone).
Upstream pre-flight for issue we-promise/sure#4407, on a branch cut from upstream main. Upstream PR: we-promise/sure#4409, from the same branch and commit.
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.
Two more gaps in that realised list, found while fixing the gate:
- It read
Current.family.tradeswith no per-user scope, while the gate and the rest of the card (investment_accounts,current_holdings) are limited to the accounts the user includes in their finances. A user therefore saw another member's unshared sales on their card. - It counted an excluded sale, although
InvestmentStatement::Totals(the same card's contributions and withdrawals) and the transactions breakdown both filterentries.excluded = false.
Change
One file, app/controllers/reports_controller.rb:
- One relation for "a disposal". The card's sell-trade query moves out of
build_gains_by_tax_treatmentinto a privateperiod_sell_trades: the period, included in reports, negative quantity, transfers out excluded, and now also not excluded and on an account the user includes in their finances (Current.user.finance_accounts, the scopeinvestment_accountsapplies).build_gains_by_tax_treatmentadds itsincludesand loads it. - The gate also opens on a disposal. It is now
investment_accounts.any? || period_sell_trades.exists?. The gate and the realised list read the same relation, so they cannot disagree on what counts as a disposal. TheEXISTSruns only wheninvestment_accountsis empty.
This answers "is there anything to show" from upstream's own data. No new model.
User-visible changes.
- A family whose only investment account is closed now sees the card in a period with a disposal, with that disposal listed. This was the reported defect.
- The realised list no longer shows another member's sales from an account not shared into this user's finances. This applies to every user, including one with an open brokerage account, who saw those sales on
main. A sale on an account shared withinclude_in_financesis still listed. - The realised list no longer shows an excluded sale, and an excluded sale does not open the card.
Changes 2 and 3 alter the card's realised figures for affected users. They were added in e2784b5f2, after the full Gatekeeper review of d330a8695.
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 countsContributionandWithdrawaltransactions, not trades, and is unchanged. For a closed-account family it shows only when the period also has such a transaction. 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.- 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.
- Who reads
period_sell_trades: the gate andbuild_gains_by_tax_treatment, nothing else.
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.current_holdingsis already scoped throughinvestment_accounts; the transactions breakdown's trade query already filtersexcludedand scopes to finance accounts. Both match the rule now applied here.Portfolio::RealizedGains(we-promise/sure#4405, fork pre-flight jaysbeekay/sure#534) is the other upstream-bound reader of disposals. It now filtersentries.excluded = falsetoo, so the two agree. Its per-user scope is its caller's, as it reads only the accounts it is given.- The other
qty < 0readers (InvestmentStatement::Totals,Portfolio::FlowClassifier) measure flows, not this card's disposals. They are unchanged. entries.excludedhasdefault: falseand is nullable. The filter isexcluded: false, the predicateInvestmentStatement::Totalsuses, so the card agrees with itself; no writer inapp/sets it toNULL.
Tests
test/controllers/reports_controller_test.rb, 11 new tests.
The gate (from 48b68b9a6 and d330a8695):
- 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.
- 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.
The list (from e2784b5f2):
- The card does not list another member's unshared disposal (closed-account family).
- The card does not list another member's unshared disposal beside an open account.
- The card lists a disposal from a member's account shared into the user's finances.
- An excluded disposal from a closed account does not show the card.
- The card does not list an excluded disposal.
The disposal tests use the default report period, which is the current month (default_start_date is Date.current.beginning_of_month), and date the sale on its first day; that dependency is what they exercise.
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 five list tests were run against d330a8695's controller (the gate fix, before the list was scoped): 53 runs, 4 failures, the four negative tests (both unshared-disposal tests and both excluded tests). The shared-account test passes there and here: it guards that the scope does not drop a shared account.
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 |
period_sell_trades drops the finance_accounts predicate | another member's closed account does not show the card; both unshared-disposal list tests |
period_sell_trades drops excluded: false | both excluded tests |
All 7 were caught. The last two were run on e2784b5f2.
Validation
test/controllers/reports_controller_test.rbone2784b5f2: 53 runs, 195 assertions, 0 failures, 0 errors.- Full suite on
d330a8695: 12652 runs, 58468 assertions, 0 failures, 0 errors, 36 skips. One2784b5f2, CI'stest_unit(the full suite) is green. 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, and now also applied to the realised list;
- 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:
