Pick the accounts that fund the retirement plan

JA
3d ago
+203-4

Part of #5567

Fork pre-flight: jaysbeekay/sure#1031 (same branch, same commits)

Stacked on #5755

Problem

A retirement plan can already be funded from accounts the user picks (RetirementPlan::FundingAccount, #5610): linked accounts replace the default set of cash, investment and crypto accounts. The settings never offered a way to pick them, so the default set was the only one anyone could use. @dgilperez raised this on #5567.

Resulting behaviour

The settings modal gets a "Fund the plan from" list: every cash, investment and crypto account the user counts in their finances (RetirementPlan#eligible_funding_accounts), with the linked ones ticked. With none ticked, the plan uses all of them, as it does today.

  • A save replaces the links in the same transaction as the settings and the first seeding, so a refused save links nothing.
  • The form always sends the list, with a blank first entry, so unticking the last account still reaches the server and returns the plan to the default set. A save that does not send the list (an older form) leaves the links alone.
  • Only accounts the plan can draw on are linked. Any other id, such as a property or another family's account, is dropped.
  • A refused save re-renders with the accounts that were ticked, not the saved ones.

No model change: this uses the funding model #5610 already has.

Size: 46 non-test lines (controller 30, form 14, locale 2).

Validation

  • Full suite bin/rails test at this head 7110d042f: 18749 runs, 1 failure, 8 errors. The failure is AccountsControllerTest "credit card overview shows the credit label when a credit outlives the due date", which depends on today's date and fails the same way at #5610's old head 158640ad4; the 8 errors are ReleaseNotesWorkflowTest, which needs node, missing from the test image.
  • Targeted: test/controllers/retirement_plans/funding_accounts_test.rb (listing with ticks, linking, replacing, none ticked, a list not sent, ineligible ids, a refused save, a refused save keeping the ticks) and a system test that ticks an account, saves, then unticks it and saves.
  • bin/rubocop and erb_lint: clean.
  • Mutations, each caught by a failing assertion: the link step skipped; the old links not removed; the eligibility filter dropped; a save without the list clearing the links; the saved links not ticked; ineligible accounts listed; the blank entry dropped (caught by the system test); the submitted ticks ignored on a refused save.

Blast radius

RetirementPlansController#update and the settings form. #save_settings gains one step inside its existing transaction. The funding total, projection, simulation and Monte Carlo already read the links.

Migration notes

No migration.

Screenshots

The bottom of the settings modal. Before:

DesktopPhone
Light
Dark

After, with one account ticked:

DesktopPhone
Light
Dark

Summary by cubic

Adds a way to pick which accounts fund a retirement plan. The settings modal now lists every eligible cash, investment, and crypto account with the linked ones ticked; ticking none uses all of them as before.

  • A save replaces the funding links in the same transaction as the settings, so a failed save links nothing.
  • Unticking all accounts sends an empty list (with a blank entry) and returns to the default set.
  • Only accounts the plan can draw on are linked; other ids are dropped.
  • A refused save re-renders with the submitted ticks, not the saved ones.
  • A save of a saved plan locks its row before writing, so two saves at once replace rather than merge the funding links.
  • Asset balances are converted before the lock is taken, so a provider call for a missing exchange rate never runs under it.

Summary by CodeRabbit

  • New Features
    • Retirement-plan settings now let you choose which eligible accounts fund the plan. Clearing the selection restores the default set of cash, investment, and crypto accounts.
    • Your selected accounts remain checked when settings are redisplayed, including after a save is refused.
  • Bug Fixes
    • Account selections are saved only for eligible accounts, and existing selections are preserved when the account list isn’t submitted.