Keep a split part's own state when the split is edited

JA
5d ago
+420-40

Part of jaysbeekay/sure#955 (part A; part B follows we-promise/sure#5809)

Upstream pre-flight. Branch up/split-edit-keeps-parts: three commits (06cdce795, 029d7af8c, a2c1390c7) on upstream main e0d9c9c16, merged with upstream main 12cd4e1a5 in 508877954 and with upstream main 19cdb5a75 in 8751a165f (clean; full unit suite on 8751a165f: 19,555 runs, 0 failures, the 8 ReleaseNotesWorkflowTest errors only).
Upstream issue: we-promise/sure#5545
Upstream PR: we-promise/sure#5933 (draft), same branch, same commit.

Scope: part A of the issue's Triage Plan only. Part B (whether a split part can be a transfer leg) is left out: it is being settled upstream by we-promise/sure#5809 (nielseulink, open), which takes the issue's option (ii).

Summary

Saving the split editor destroyed every part and built new ones from the form. A part lost its activity label, notes, attachments, attribute locks and exclusion, even when the edit didn't touch it. Now a part that is still in the split is updated in place and keeps everything the form doesn't set.

What changed

  • app/views/splits/edit.html.erb, _row.html.erb: each existing row posts its part's entry id in a hidden field. A row added in the dialog posts none; the create dialog posts none.
  • SplitsController: permits id and passes it through build_splits.
  • Entry#resplit!: when any split hash carries an id, update_split_parts! runs under the existing split lock:
    • ids are looked up through child_entries only; an id that isn't one of this split's parts, or that appears twice, raises RecordInvalid and nothing changes;
    • the amounts must still sum to the parent;
    • refuse_while_split_part_transferred! runs first, as unsplit! does, so a part that is a confirmed transfer leg still blocks the edit (we-promise/sure#4966, merged);
    • left-out parts are destroyed, kept parts get name, amount, category, merchant and tags, and rows without an id become new parts built the same way split! builds them.
    • With no ids at all (API, rules, the MCP tools in we-promise/sure#4747), it still replaces every part.
    • A kept part whose tag ids change gets its entry touched. Tags are saved as taggings alone, so without the touch a tag-only change saved neither the transaction nor the entry, Family#entries_cache_version stayed put, and a tag-filtered search's cached totals were served stale. The old destroy-and-recreate always moved the version, and so does the transaction form (its lock_attr! write).
  • Api::V1::SplitsController#update: comment only. It no longer says parts are replaced "exactly as the web UI does it"; the API still replaces.
  • Entry#split!: no behaviour change. Its total check and part builder moved into ensure_split_total! and create_split_part! so the in-place path reuses them.

Decided against the plan's wording in one place: the plan lists excluded among the fields a kept part takes from the form. The edit form has no exclusion field, so excluded arrives as nil; writing it would clear an exclusion set from the transaction drawer, the same loss this fixes. A kept part's exclusion changes only when the hash sets excluded.

Locks: a kept part keeps its locked_attributes on the entry and the transaction. A changed category or merchant on a kept part gets no new lock, the same as a new part from the web form today (split! locks only with lock_attributes: true, which resplit! doesn't pass). If we-promise/sure#5492 lands, it should apply its rule to both paths.

Tests

TestBefore the fix
EntrySplitTest "resplit! keeps a kept part's label, notes and attachment by id": remove part 1, keep part 2 by id with a new name and amountRecordNotFound: part 2 was a new record
"resplit! leaves a kept part's locks alone" (entry and transaction locks)RecordNotFound
"resplit! by id refuses while a kept part is a confirmed transfer leg": neither leg's amount changespasses both ways (the old path refused through unsplit!); guards the new path
"resplit! by id keeps a pending match, which then can't be confirmed with unequal amounts": transfer stays pendingRecordNotFound
"resplit! refuses a part id from another split and changes nothing"nothing raised
"resplit! refuses the same part id on two rows"nothing raised
"resplit! by id still requires the parts to sum to the parent"passes both ways; guards the new path
"resplit! by id creates a row with a blank id as a new part like split! does": labelled parent, label compared to what split! gives, so it holds when we-promise/sure#5507 landsfails: part 1 is replaced too
"resplit! by id keeps a kept part's exclusion when the row doesn't set it"RecordNotFound
"resplit! by id updates a kept part's category, merchant and tags"RecordNotFound
"resplit! by id refuses to remove a confirmed transfer leg": leaves the leg out of the rows; part, transfer and the other part's amount survivepasses; guards the refusal's place (see Mutations)
"resplit! by id changes nothing when a kept row fails validation after a removal" (blank name)passes; guards the rollback of the removal
"resplit! by id moves the family's entries cache version on a tag-only change"at 029d7af8c: version unchanged (8-1795691064.4480782-… both sides)
"resplit! by id moves a tag-filtered search's totals cache key on a tag-only change": tagged count +1at 029d7af8c: cache_key_base unchanged
SplitsControllerTest "edit posts each existing part's id and none for a new row"no id fields
"update keeps a part's activity label" (through the form)RecordNotFound
"update refuses a part id from another split"no alert

The existing "resplit! replaces the children of a split" (no ids) still passes unchanged.

Mutations

One at a time. The first ten ran against the two test files (71 runs); the last three against entry_split_test.rb:

MutationCaught by
Always replace all parts10 tests
Match rows to parts by position, not idlabel/notes/attachment test, both foreign-id tests
Drop the id checkforeign-id (model, controller) and duplicate-id tests
Drop the transfer-leg refusal on the in-place pathconfirmed-leg test
Always overwrite excludedexclusion test
Drop the sum check on the in-place pathsum test
Form omits the hidden idedit-form id test
Controller drops idboth controller update tests
Kept part's category/merchant/tags not updatedfield-update test
Kept part's amount not updated3 tests
Transfer-leg refusal moved below the destroy loopremove-a-leg test only (SplitPartTransferredError expected but nothing was raised): destroying the part cascades its transfer
with_split_lock without its transactionrollback test only (child ids differ)
Tag-change touch droppedboth cache tests

An upper-case copy of a part id also matches that part (probe; the uuid type casts it to lower case).

Verification

  • Full unit suite on a2c1390c7: 19,457 runs, 0 failures, 8 errors, 9 skips. The errors are ReleaseNotesWorkflowTest (no node in the test image).
  • Split tests, web and API controllers on a2c1390c7: 118 runs, 0 failures.
  • System: test/system/splits_test.rb, transfers_test.rb: 5 runs, 0 failures, re-run on a2c1390c7. "edit dialog saves the merchant and tags picked with the DS pickers" now goes through the in-place path.
  • RuboCop (4 files; 3 changed since, re-run): no offences. erb_lint (2 files): no errors. Brakeman: no warnings. No JavaScript changed.

Screenshots: none. The only markup change is a hidden input, so nothing visible changes.

Blast radius

  • resplit! callers: SplitsController#update (now sends ids), Api::V1::SplitsController#update (permits no id, so it keeps replace-all), and Assistant::Function::SplitTransaction in open we-promise/sure#4747 (no ids, replace-all).
  • split! callers, behaviour unchanged: split_once! (web create, API create, Rule::ActionExecutor::SplitTransaction), Family::DataImporter#import_split_lines!, QifImport. The web create form posts no ids, and create_split_part! ignores :id.
  • _row.html.erb is shared with new.html.erb; id defaults to nil, so the create dialog renders no hidden field.
  • Open upstream PRs on the same code: we-promise/sure#5492 (adds lock_attributes: to resplit!), we-promise/sure#5507 (parent's label on new parts, in split!), we-promise/sure#5736 (moving a split's date in bulk_update!), we-promise/sure#5809 (transfer legs). None changes resplit!'s replace logic; whichever lands first, this rebases onto.