Let the planner's spending and income streams be edited

JA
4d ago
+477-8

Base: up/retirement-plan-ui-card (stacked) | Head: up/retirement-plan-ui-streams

Part of #5567

Fork pre-flight: jaysbeekay/sure#975 (same commits)

Stacked on #5753

Fourth of five UI PRs: the spending and income a plan draws on.

Problem

The first settings save seeds a plan's living costs and loan repayments. The page cannot show them, though, and a user cannot add a pension, a one-off cost or spending that ends, which are the inputs the year-by-year plan exists to model.

Resulting behaviour

The planner lists the plan's streams in a DS::Table: name, type, years and the amount a year. Each row has a DS::Menu with Edit and Delete. Delete asks first.

  • Adding or editing a stream happens in its own modal: type (spending, income such as a pension, or a one-off), name, amount a year, from and to year, and whether it rises with inflation.
  • Refusals and saves follow the app's modal-form pattern, as the settings form does: a refusal re-renders inside the modal, and a save redirects the planner.
  • Tenancy: streams are always found through the signed-in user's own plan, so another user's stream id gives 404.
  • A user's first stream also saves their plan, in one transaction, so an invalid stream leaves no empty plan behind. A second first save that loses the race on the one-plan-per-user index adds its stream to the plan the other created.
  • One-offs show the year they are paid ("once, 2030"), not a span, because a one-off is paid once, in its start year.
  • A seeded stream given another type counts as the user's own line. Before retirement the simulation leaves seeded living costs and loan repayments out, because the contribution already nets the last 12 months' spending. A seeded stream changed to income or a one-off is no longer today's spending, so a pension made from it counts before retirement from its start year. Changed back to spending, it is seeded again and left out as before, so nothing is counted twice. The stream keeps its seeded source throughout; the type decides how it is read.
  • The planner explains the two spending figures. A line above the streams says the FI number follows the last 12 months' spending, while the streams drive the year-by-year path, so editing a stream moves the path and not the FI number.

Size: 211 non-test lines added, 6 removed.

Validation

  • Full suite bin/rails test at this head a065de3d4: 18707 runs, 0 failures. The 8 errors are ReleaseNotesWorkflowTest, which needs node, missing from the test image.
  • System tests (test/system/retirement_plans_test.rb, 4 runs, 0 failures at a065de3d4): adding a pension from its modal; a refused stream stays in the modal over the planner.
  • bin/rubocop, erb_lint and Brakeman: clean.
  • Preview gate: off redirects for the stream pages and a create, with nothing written.
  • Tests for the seeded streams and the explainer:
    • a seeded stream whose type is changed counts before retirement (a 12,000 pension adds at least 12,000 to next year's paid-in figure, and the source stays seeded);
    • a seeded stream whose type is changed and changed back is seeded again (source back to seeded_living_costs, and next year's paid-in figure equal to the never-changed case);
    • an edit that keeps the type leaves a seeded stream seeded;
    • a stream is seeded spending only while it is a seeded expense (seeded living costs and loan as spending; not as income or a one-off; never a manual stream);
    • the planner explains the FI number is from recent spending, not the streams.
  • Mutations, each caught by a failing assertion:
    • the streams controller without require_preview_features!;
    • a stream looked up outside the user's own plan;
    • a one-off shown as a span;
    • the first-save race not rescued;
    • a seeded stream read as seeded whatever its type;
    • any expense read as seeded, manual ones included;
    • a type change rewriting the source to manual, which loses it on the way back.

Blast radius

New nested routes and controller, the stream views, and a section of the planner page. RetirementPlan::Stream#to_simulation_stream now marks a stream seeded only while a seeded source is an expense; it is the only reader of source, and seeding is gated by streams_seeded_on, not by source, so a changed stream is never seeded again. In retirement_plan.rb only the simulation_inputs comment changes, to say the FI number keeps following the last 12 months' spending. No other model changes.

Migration notes

No migration.

Screenshots

Before: the planner from the previous PR, with no streams section:

Planner before streams

After, desktop and phone:

Planner with streams

Planner with streams, phone

Adding a stream:

Add stream modal

The FI-number explainer above the streams, before and after, light and dark:

Streams before the explainer, light

Streams with the explainer, light

Streams before the explainer, dark

Streams with the explainer, dark


Summary by cubic

Lets the planner's spending and income streams be edited. The page previously couldn't show the living costs and loan repayments seeded on the first settings save, nor let a user add a pension, a one-off cost, or spending that ends.

The planner lists the plan's streams in a table — name, type, years, amount — with Edit and Delete per row. Adding or editing happens in a modal: type (spending, income, one-off), name, amount, from and to year, and whether it rises with inflation.

  • A refusal re-renders the form inside the modal; a save redirects the planner.
  • Streams are always found through the signed-in user's own plan, so another user's stream id gives 404.
  • A user's first stream also saves their plan in one transaction, so an invalid stream leaves no empty plan behind; a lost race on the one-plan-per-user index adds the stream to the winner's plan.
  • A one-off shows the year it is paid ("once, 2030") rather than a span, its To year says to leave it blank, and its amount is not called yearly.
  • Streams that don't rise with inflation are tagged "fixed amount" in the list.
  • A stream stays seeded only while a seeded source is an expense; a seeded stream changed to income or a one-off counts from its start year, and is seeded again if changed back. The planner notes the FI number is from recent spending, not the streams.

Summary by CodeRabbit

  • New Features
    • Retirement plans now support adding, viewing, editing, and deleting income and spending streams.
    • Stream details include annual amounts, timing, and whether amounts are indexed. One-off streams show their year, while recurring streams show their year range.
    • Add and edit forms provide fields for stream details, and empty plans show guidance for adding a stream.
    • The planner explains how post-retirement stream spending relates to the FI number, which continues to use spending from the last 12 months.
  • Bug Fixes
    • Invalid stream entries display errors without saving, and changes to streams belonging to another user are blocked.