← Back to Pull requests

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

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 29, 2026 10:32 UTC
Author

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

    | Test | Before 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 amount | RecordNotFound: 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:

    | Mutation | Caught by |
    |---|---|
    | Always replace all parts | 10 tests |
    | Match rows to parts by position, not id | label/notes/attachment test, both foreign-id tests |
    | Drop the id check | foreign-id (model, controller) and duplicate-id tests |
    | Drop the transfer-leg refusal on the in-place path | confirmed-leg test |
    | Always overwrite excluded | exclusion test |
    | Drop the sum check on the in-place path | sum test |
    | Form omits the hidden id | edit-form id test |
    | Controller drops id | both controller update tests |
    | Kept part's category/merchant/tags not updated | field-update test |
    | Kept part's amount not updated | 3 tests |
    | Transfer-leg refusal moved below the destroy loop | remove-a-leg test only: destroying the part cascades its transfer |
    | with_split_lock without its transaction | rollback test only |
    | Tag-change touch dropped | both 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.

    <!-- This is an auto-generated description by cubic. -->
    ---

    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.
  • <sup>Written for commit 5877fb40198ec1378c8222645d6bfb97067b3807. Summary will update on new commits.</sup>

    <a href="https://cubic.dev/pr/we-promise/sure/pull/5933?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a>

    <!-- End of auto-generated description by cubic. -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    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.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jaysbeekay Review duration: 8892m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #5933"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%