Group the allocation by what the classification slices write

Merged
JA
2mo ago
+436-9

Slice 3.4 of #122 (IP3 — classification and allocation). Triage plan: #122 comment.

Fork-only. 3.3 onward is UI, which under the revised delivery model in #118 stays on fork main behind the preview gate rather than going upstream as it is built.

What this is

The allocation section already groups four ways — security account currency kind — and none of them can group by anything #192 (3.2) and #193 (3.1) write. Until this lands the classification columns have no consumer, so nobody can see whether what those slices write is right.

Four groupings are added: asset class, sub-class, sector, region. The donut, segmented control, colour palette, weight arithmetic and table all keep working — this extends a working section rather than replacing it.

The Unclassified bucket is load-bearing, not cosmetic

Portfolio::SectionRegistry gates the section on allocation_segments.any?. A grouping returning [] would hide the whole allocation section rather than show an empty one.

So without UNCLASSIFIED, a portfolio of unclassified securities loses the section instead of being told it has nothing classified yet — and that is every portfolio until #192 and #193 merge. Which is also what this looks like today: one Unclassified segment carrying the full value. That is correct behaviour, and a test asserts it directly rather than leaving it to be discovered.

Where the cash goes differs per grouping

Cash is not a holding — it sits on the account, and allocation_by_currency/allocation_by_kind already add each account's positive cash separately. Every new grouping has to say where it goes, and the honest answer is not the same for all four:

groupingcash lands inwhy
asset classliquiditya cash balance is liquidity
sub-classcashlikewise
sectorUnclassifiedcash has no sector
regionUnclassifiedcash has no region

The obvious implementation puts it in liquidity everywhere; the test pins both directions, because inventing a sector for cash puts an exposure in the chart the family does not have.

The drill-down

Asset class → sub-class → holdings, as a <details> disclosure — native HTML per convention 3, not a new Stimulus controller.

Only that ladder nests. A sector has no sub-sector here, so sector, region, currency and account render flat rather than being dressed up as something that opens. Expanding Liquidity shows the account cash, or the children would contradict the parent row's amount.

Blast radius

  1. ALLOCATION_GROUPINGS is a whitelist, not just a list. PortfoliosController:21 reads params[:by].presence_in(ALLOCATION_GROUPINGS), so a new entry is reachable by URL the moment it is in the constant. The locale keys land in the same commit for that reason, and a controller test asserts each new grouping renders rather than 500s.
  2. allocation_segment_name returned segment.name raw for everything but kind. The new groupings carry keys (equity, north_america, unclassified), so each is translated — with default: fallback, since sector is provider free text with no key and should show the value rather than "translation missing".

Evidence

  • 10 tests — per-grouping amounts and weights, the Unclassified bucket on an entirely unclassified portfolio, cash placement both ways, a partly classified portfolio whose weights still sum to 100, the drill-down at both levels, Liquidity expanding to its cash, and the flat groupings returning no children.
  • 4 mutations, all caught: Unclassified fallback removed, cash given a sector, a grouping dropped from the constant, the asset-class cash bucket removed.
  • 309 runs / 1293 assertions across statement, controller, portfolio models and helpers — 0 failures.
  • Full suite: 9901 runs, 41229 assertions, 0 failures, 1 error — the PG::ConnectionBad in RecurringTransaction::PipelineTest, which is issue #168 and reproduces on branches carrying none of these changes.
  • Rubocop, erb_lint and Brakeman clean. en and de locale keys.

Leaves out

3.5 (tags), 3.6 (AI proposals), 3.7 (fund look-through), and any change to how weights are computed. Sorting stays by amount, largest first, rather than adding per-column sort to a section that has never had it.