Lock the categories and merchants chosen when splitting a transaction

DX
8d ago
+160-12

Fixes #3568

What was broken
A category picked for a part in the split dialog wasn't treated as a manual choice. SplitsController#create called split_once! and #update called resplit! without lock_attributes: true, so the parts' categories and merchants were left unlocked. On the next family sync, any active rule matching a part replaced its category, for example putting a "Reimbursable" part back in "Gas".

A category set by hand anywhere else is locked: the category picker, the edit form, bulk edit and the transactions API all lock the attributes they change. The split rule action already passes lock_attributes: true for the parts it creates. Only the dialog and the splits API didn't.

so i changed this all

SplitsController#create and #update pass lock_attributes: true, so the category and merchant chosen for each part are locked the way split! already supports.
Entry#resplit! takes the same lock_attributes: option and passes it to split!. It defaults to false, so other callers are unchanged.
The splits API (POST and PATCH /api/v1/transactions/:id/split) locks them too, matching the transactions API, which locks the attributes an update changes.
A part left without a category, or with its merchant falling back to the parent's, stays unlocked, so rules can still fill it in. An explicit Apply or Apply all rules run (ignore_attribute_locks: true) still overrides the lock, as it does for any other manual choice.

QIF and Sure-export imports also split transactions, but those splits aren't manual choices, so they're unchanged.

new tests:
test/controllers/splits_controller_test.rb
a rule run leaves a category chosen in the split dialog alone: the issue's scenario. A merchant rule sets a category, a transaction from that merchant is split with one part given a different category, then the rule is applied.
create locks the category and merchant chosen for each part
update locks the categories chosen for the new parts
test/controllers/api/v1/splits_controller_test.rb
locks the category and merchant it assigns to each child
locks the categories of replacement children

All five fail on main. To check the coverage, I removed each change in turn: the lock on dialog create, dialog update, API create, API update, and the option passed through in resplit!. Each removal made at least one of the new tests fail.

Run locally (Ruby 3.4.13, PARALLEL_WORKERS=1):

bin/rails test test/controllers/splits_controller_test.rb test/controllers/api/v1/splits_controller_test.rb: 67 runs, 227 assertions, 0 failures, 0 errors, 0 skips. On main with only the new tests: 67 runs, 5 failures (the five above).
bin/rails test test/models/entry_split_test.rb test/models/rule_test.rb test/models/rule/action_executor/*_test.rb test/models/rule/runner_test.rb test/models/entry_test.rb test/controllers/transactions_controller_test.rb: 314 runs, 1176 assertions, 0 failures, 0 errors, 0 skips.
bundle exec rspec spec/requests/api/v1/splits_spec.rb: 485 examples, 0 failures, 89 pending.
bin/rubocop on the five changed files: no offenses.


Summary by cubic

Locks the category and merchant chosen when splitting a transaction so rule runs no longer overwrite them.

A category picked in the split dialog wasn't treated as a manual choice, so on the next family sync a matching rule could replace it, for example moving a "Reimbursable" part back into "Gas". The dialog and the splits API now pass lock_attributes: true through split_once! and resplit!, matching how the category picker, edit form, bulk edit, and transactions API already lock attributes. The splits API schema now documents that sent categories and merchants are locked.

  • Parts sent without a category, or a merchant matching the parent's, stay unlocked so rules can still fill them in.
  • An explicit rule apply (with ignore_attribute_locks: true) still overrides a locked choice, as it does elsewhere.
  • QIF and Sure-export imports are unaffected.

Summary by CodeRabbit

  • Bug Fixes
    • Categories and merchants explicitly selected for split entries are preserved against later rule runs.
    • Split entries without a selected category remain eligible for category rules.
    • Replacing or updating a split preserves selected categories and merchants; a merchant inherited from the parent remains unlocked.
  • Documentation
    • Clarified how supplied, omitted, or null child categories and merchants are handled, including which selections are locked against later rule runs.