Let a family tag a security with its own tags
Part of #3494
Fork pre-flight: jaysbeekay/sure#537, from the same branch.
Updated 2026-11-05. #4081's head is merged down, bringing upstream main (529d7de76). No change to this PR's own files. To review this slice alone: up/taggings-unique-index...up/security-taggable. Full suite on 5663fba74: 14,709 runs, 0 failures, 0 errors.
Draft: this is slice U4 of the classification programme in #3494, the model half of letting a household tag a security with its own tags. No UI.
Stacked on #4081 (unique index on taggings). Until #4081 merges, this PR's diff also shows its commits; the commit on top is this change.
Summary
The classification columns on securities (asset class, sub-class, sector, region) live on a row that every family holding the instrument shares, so they cannot hold a grouping that means something to one household only, such as "pension", "kids' fund" or "speculative". tags already carries family_id and taggings is already polymorphic, but only Transaction is a taggable.
This makes Security a taggable too, with one writer that keeps the edit inside the family that made it.
What changed
app/models/security.rb
has_many :taggings, as: :taggable, dependent: :destroyandhas_many :tags, through: :taggings.Security#set_tags_for(family, tag_ids): replacesfamily's tags on the security and leaves every other family's alone.
app/models/tag.rb
has_many :securities, through: :taggings, source: :taggable, source_type: "Security", mirroring the existingtransactions.
Tenancy: why the writer is not security.tags = ...
A security is global, a tag is per family. security.tags therefore spans every family that has tagged the instrument, and the obvious writers are wrong in two ways:
security.tags = family.tags.where(id: tag_ids) # deletes other families' taggings
security.tag_ids = tag_ids # attaches a tag the family does not own
The first destroys another household's data. The second discloses: attaching a tag this family does not own would put that tag's name on an instrument other families hold. set_tags_for resolves ids only through family.tags (an id from another family, or an unknown id, is ignored), removes only taggings whose tag belongs to family, and creates only the missing ones. Each failure mode has its own test.
Reading one family's tags is security.tags.where(family: family). Nothing reads security.tags yet; the association comment says which form to use.
with_lock serialises edits to one security. With #4081's unique index, two concurrent saves of the same tag would otherwise fail one of them on the index rather than both succeed. The lock reloads the record, so an association the caller had already loaded does not keep a removed tag.
Left out: any UI or controller action (held for the UI phase), tagging a Holding (a holding row is a per-date snapshot, so a tag on it would be lost on the next materialisation), per-account tagging of one instrument, allocation by tag, and export/import of security tags (Family::DataExporter exports transaction tag ids only; unchanged).
Demo data
No change to demo data or seeds.
Migration notes
None. No migration and no schema change: taggings.taggable_type is a free string, and #4081's index already keys on it.
Verification
- New tests:
test/models/security/tags_test.rb(11 tests): attach and reach from the tag, another family's tag not attached, another family's tags survive an edit, an empty selection clears only this family's, a tag left out is removed and the kept tagging row is untouched, a repeated id keeps the one existing row, already loaded associations reflect the write, destroying a tag, destroying a security, merging two tags on the same security, and a family data reset. - Fail-first. Against #4081's head without this change: 11 runs, 11 errors, each
NoMethodErroronset_tags_for,taggingsortagsforSecurity. - Mutations, each applied alone to the committed change,
test/models/security/tags_test.rbrun, then reverted:
| Mutation | Caught by |
|---|---|
Naive self.tags = family.tags.where(id: wanted) | "leaves another family's tags on the security alone", "an empty selection clears this family's tags but not another family's" |
Ids resolved through Tag instead of family.tags | "a tag belonging to another family is not attached" |
mine taken from Tag (removal not scoped to the family) | "leaves another family's tags...", "an empty selection clears..." |
| No removal of deselected tags | "a tag left out of the selection is removed...", "an empty selection clears...", "already loaded associations..." |
| Remove all of the family's taggings and recreate | "a tag left out ... the kept one is untouched", "re-submitting the same tag keeps the one existing tagging" |
| Create every wanted id, no check for existing | the same two tests (RecordNotUnique from #4081's index) |
Bare transaction instead of with_lock | "already loaded associations reflect the write without a reload" |
Drop dependent: :destroy on Security#taggings | "destroying a security removes its taggings and keeps the tags" |
Drop Tag#securities | "set_tags_for attaches the family's tag and the tag reaches the security" |
The merge and reset tests pin existing code (Tag#replace_and_destroy! from #4081, Family::FinancialDataReset) against the new taggable type; they are regression coverage, not fail-first evidence for this change. The concurrency half of with_lock is not exercised by a test.
- Neighbouring suites (
test/models/security/tags_test.rb,security_test.rb,tag_test.rb,tagging_test.rb): 50 runs, 0 failures. - Full
bin/rails teston this branch (Ruby 3.4.9): 11858 runs, 54621 assertions, 0 failures, 0 errors, 34 skips, at6cf322131. bin/rubocopon the changed files: no offenses.bin/brakeman: no warnings. No ERB changed.- Merges cleanly with current
main(git merge-tree).
Blast radius
Who else hits these lines, and what do they pass? set_tags_for is new and has no caller yet. The associations add a second taggable type to the readers of taggings:
Tag#replace_and_destroy!movestaggingswith no type filter, so merging two tags now moves security taggings too. That is intended. With #4081, a security already carrying both tags keeps one tagging instead of gaining a duplicate; tested.Tag has_many :taggings, dependent: :destroy, andFamily has_many :tags, dependent: :destroy, so deleting a tag or a family removes its security taggings.Family::FinancialDataResetscopes taggings asTagging.where(tag_id: tag_scope.select(:id)), so a reset removes that family's security taggings and leaves other families' on the same security; tested.Demo::DataCleaner(Security.destroy_all) andlib/tasks/securities.rake(security.destroy!) now also remove the security's taggings, throughdependent: :destroy.
What else reads what this changes the reading of? grep -rn "taggings\|Tagging\|taggable" app/ lib/:
Rule::ConditionFilter::TransactionTagfilterstaggable_type: "Transaction"; unaffected.ReportsControllerjoins taggings from the transaction side; unaffected.Family::DataImporterandQifImportcreate taggings on transactions only; unaffected.Family::DataExporterreadstransaction.tags/tag_ids; security tags are not exported (left out above).
Summary by CodeRabbit
-
New Features
- Securities can now be tagged, with each family able to view and manage only its own tags.
-
Bug Fixes
- Duplicate tags are no longer applied when assigning tags or importing transactions, including split transactions.
- Existing duplicate tag associations are cleaned up, and duplicates are prevented going forward.
- Replacing a tag no longer creates duplicate associations when the replacement is already present.
- Invalid tag IDs are rejected when creating or updating transactions; rejected updates leave existing tags unchanged.
- Submitting a blank tag ID still clears a transaction’s tags.