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

JA
5d ago
+420-40

Closes #5545

Fork pre-flight: jaysbeekay/sure#1004, same branch (up/split-edit-keeps-parts), same commit (8751a165f). Three commits on main e0d9c9c16, then merges of main 12cd4e1a5 and 19cdb5a75, both clean.

This covers the bug in #5545. The issue's second question, whether a split part can be a transfer leg, is left to #5809, which answers it.

Problem

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.

What changed

A part that is still in the split is now updated in place and keeps everything the form doesn't set.

  • 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, and neither does the create dialog.
  • 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 (#4966).
    • Parts left out are destroyed. Kept parts get name, amount, category, merchant and tags. Rows without an id become new parts, built the same way split! builds them.
    • With no ids at all (API, rules), 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.
  • 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.

Exclusion. The edit form has no exclusion field, so excluded arrives unset. 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 #5492 lands, its rule should apply 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"passes 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"RecordNotFound
"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"fails: 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"passes; guards where the refusal sits (see Mutations)
"resplit! by id changes nothing when a kept row fails validation after a removal"passes; guards the rollback of the removal
"resplit! by id moves the family's entries cache version on a tag-only change"version unchanged
"resplit! by id moves a tag-filtered search's totals cache key on a tag-only change"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:

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: destroying the part cascades its transfer
with_split_lock without its transactionrollback test only
Tag-change touch droppedboth cache tests

Validation

  • Full unit suite on 8751a165f: 19,555 runs, 0 failures, 9 skips; the 8 errors are ReleaseNotesWorkflowTest, which needs node, missing in the test image.
  • System: test/system/splits_test.rb, transfers_test.rb: 5 runs, 0 failures on the pre-merge head. "edit dialog saves the merchant and tags picked with the DS pickers" now goes through the in-place path.
  • RuboCop: no offences. erb_lint: 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) and Api::V1::SplitsController#update (permits no id, so it keeps replace-all).
  • split! callers, behaviour unchanged: split_once! (web create, API create, Rule::ActionExecutor::SplitTransaction), Family::DataImporter#import_split_lines!, QifImport. The 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 PRs on the same code: #5492 (adds lock_attributes: to resplit!), #5507 (parent's label on new parts, in split!), #5736 (moving a split's date in bulk_update!), #5809 (transfer legs), #5833 and #5734 (other markup in the split dialog). None changes resplit!'s replace logic. Whichever lands first, this rebases onto.

Migration notes

No migration.


Summary by cubic

Fixes #5545. Saving the split editor rebuilt every part from the form, so a part lost its activity label, notes, attachments, locks and exclusion even when the edit didn't touch it.

The edit form now posts each existing part's id, and Entry#resplit! updates matched rows in place. A row without an id becomes a new part, and rows missing from the edit are removed. A call with no ids at all (the API, rules) still replaces every part.

  • Part ids are validated against the split's own parts; an unknown or duplicate id refuses the edit and changes nothing, and a stale id from a form left open shows a translated alert.
  • Kept parts keep excluded unless the row sets it, and keep their locks.
  • The edit refuses while a kept part is a confirmed transfer leg, and a failed validation rolls back any part removals.
  • A tag-only change on a kept part now touches the entry so tag-filtered caches aren't served stale.
  • Transfer#confirm! re-reads both legs under the lock, so a resplit that changed a kept part's amount can't be confirmed with legs that no longer offset.
  • A transfer update that fails validation now redirects with the validation messages as an alert instead of a generic 422; the rolled-back transaction saves nothing.
  • The create dialog and API are unchanged: they send no ids and still build new parts.

Summary by CodeRabbit

  • New Features
    • Editing a split now updates existing parts in place, preserving their labels, notes, attachments, locks, and exclusions when applicable. You can also add new parts or remove omitted ones.
  • Bug Fixes
    • Split edits with invalid part IDs, invalid totals, or confirmed transfer parts are rejected without changing existing parts. If a split changed elsewhere, you’re prompted to reopen it before retrying.
    • Transfer confirmation checks the latest transaction amounts, and invalid confirmations display an alert without confirming the transfer.
  • Documentation
    • API guidance clarifies that replacing split parts removes and recreates them, so callers should submit the full desired set.