Let a household tag a security with its own scheme
Part of #122 (slice 3.5). Triage plan: https://github.com/jaysbeekay/sure/issues/122#issuecomment-5741369165
The problem
The classification slices give every security the same four axes everyone else
gets — asset class, sub-class, sector, region. None of them can express a grouping
that means something only to one household: "pension", "kids' fund", "speculative",
"employer stock". taggings is already polymorphic and tags already carries
family_id, so the machinery was there and only Transaction used it.
There is a second reason this matters. 3.3 (#197) writes the classification to a
globally shared securities row. A Tag is family-scoped, so a tagging is
reachable only through the owning family's tags — tags are the family-scoped
counterpart that 3.3 could not be, and therefore the right home for anything
subjective.
A correction to the slice as written
The epic asks for "family-scoped Security/Holding taggables". Holding is
the wrong taggable. holdings is a per-date snapshot table — db/schema.rb
carries a unique index on (account_id, security_id, date, currency), and the
fixture set alone holds two rows for one security across two dates. A tagging
attached to a Holding is attached to one day's row; it would disappear the
next time Balance::Materializer rebuilt them.
So the taggable is Security only, and a test asserts exactly that by tagging,
creating the next day's holding row, and checking the tag is still there. The
practical loss is per-account tagging — the same instrument cannot be "pension"
in one account and "speculative" in another. That wants a (family, account, security) row rather than a tagging on a snapshot, and it is not what the slice
asks for.
What changed
Security has_many :taggings, as: :taggable, dependent: :destroy+has_many :tags, through: :taggings, andTag has_many :securities, through: :taggings, source_type: "Security", mirroring its existingtransactions.Security#set_tags_for(family, tag_ids)— the writer, explained below.PATCH /holdings/:id/tagsbehindrequire_account_permission!(account, :annotate)
— the same level a transaction's tags sit behind, not:write— rendering the
existingDS::TagSelectin the holding drawer.
Kept as a separate action and section from 3.3's classification, deliberately:
one writes a globally shared row and the other a family-scoped overlay. Putting
two different blast radii behind one Save button would be a mistake.
Why the writer is not a one-liner
security.tags spans every family, because the security row does. So the
obvious implementation is wrong in two independent ways, and both are cross-family
defects rather than cosmetic ones:
security.tags = family.tags.where(id: tag_ids) # deletes OTHER families' taggings
Tag.where(id: tag_ids) # attaches a tag you don't own
The first destroys data belonging to another household. The second is a
disclosure: because the security is shared, attaching a tag this family does not
own would publish that tag's name against an instrument other families hold.
set_tags_for replaces only the given family's taggings and resolves ids through
family.tags. Both failure modes have their own test, and the mutation table
below shows the naive assignment being caught by two of them.
The picker is fed from Current.family.tags for the same reason — a test asserts
another family's tag name is not rendered into the page.
Blast radius — the callers I checked rather than assumed
Tag#replace_and_destroy!(app/models/tag.rb:48) does
taggings.update_all tag_id: replacement.id, with no taggable-type filter, so
merging two tags now moves security taggings too. That is the right behaviour,
but if both tags are already on the same security it produces a duplicate
tagging row — there is no unique index ontaggings. This is pre-existing and
equally true for transactions today; I have not widened the slice to add the
index, but it is worth an issue.Family::FinancialDataResetscopes taggings asTagging.where(tag_id: tag_scope.select(:id)), so a family reset takes its security taggings with it.
Correct as-is; no orphan path introduced.Rule::ConditionFilter::TransactionTagandReportsControllerboth join
taggings from the transaction side, so neither sees securities.dependent: :destroyon the security side, so taggings cannot outlive a
security thatSecurity::Resolveror the remap path leaves unreferenced.
What this leaves out
Allocation by tag, the second half of the slice. It needed
ALLOCATION_GROUPINGS and classification_bucket, which were in #194. #194 has
since merged (2026-09-19), and the allocation-by-tag work is now open as #201
rather than waiting here. Keeping it out of this PR was so it would not stack —
a stacked PR loses its CodeRabbit review, which skips non-default bases.
Evidence
Fourteen tests in test/controllers/holdings_tags_test.rb, in three groups.
The original eleven were written before the implementation and watched to
fail — all 11 errored on the missing route first. The load-bearing ones are the
two cross-family cases, not the happy path: "a tag belonging to another family
cannot be attached" and "tagging a shared security leaves another family's tags
on it alone".
A fourteenth arrived with c2425592d, answering Codacy's drift note:
"a read-write member is offered the picker they are allowed to submit". Nothing
covered that direction before — the hidden-picker test drives a read_only
member and the save test POSTs straight to the action, so the view's permission
list could have dropped read_write with the whole suite green. The view and the
action now answer from one AccountAuthorizable::PERMISSION_LEVELS table, and
this test is what fails if they are ever answered separately again.
Two more arrived with 8dee16d99, which answered the gate's :annotate
findings rather than the original design: "a read-write member can tag a
holding" pins that a family-scoped annotation is gated at :annotate like a
transaction's tags, not at :write; "the picker is hidden from a member who
cannot annotate" pins that the drawer does not offer a control whose save would
be refused. These two were written against an implemented route, so the
fail-first claim above covers the eleven and not them.
Seven mutations, all caught:
| Mutation | Caught by |
|---|---|
naive security.tags = family.tags.where(…) | 2 tests — the other family's tag destroyed |
ids resolved from Tag instead of family.tags | the disclosure test |
stale tags never removed (no destroy_all) | empty-selection clears |
| duplicates allowed (create every wanted id) | re-submitting does not duplicate |
picker fed from Tag.alphabetically | picker lists only this family's tags |
drop the :annotate gate | read-only member refused |
| view's permission list drifts from the action's | read-write member is offered the picker |
| remove the picker from the drawer | 2 UI tests |
One test needed correcting during the run: it asserted Tagging.count moved by
-1 when a tag was destroyed, and it moved by -2 — the fixture tag also tags a
transaction. It now scopes the assertion to Tagging.where(taggable: @security),
so it asserts this change rather than the fixture set.
Checks:
- Full suite: 9948 runs, 41544 assertions, 0 failures, 0 errors, 33 skips.
(Clean throughout — theRecurringTransaction::PipelineTesterror present on
earlier branches is gone now that #196 is onmain.) bin/rubocop: 2773 files inspected, no offenses.erb_linton the changed template: no errors.bin/brakeman --no-pager: 0 security warnings, 0 errors.