Add BalanceSheet::NetWorthVelocity for net worth pace

Closed
JA
2mo ago
+346-0

Closed as merged upstream, 2026-10-12, on the owner's instruction ("Close #379 and #360 as merged upstream"). we-promise/sure#4083 was merged into upstream main as b6f6c3211 "Add BalanceSheet::NetWorthVelocity for net worth pace (#4083)". It is on mirror/upstream-main at d55eac220.

  • Checked before closing: both files this branch changes (app/models/balance_sheet/net_worth_velocity.rb, test/models/balance_sheet/net_worth_velocity_test.rb) are identical between this PR's head d76ba7038 and upstream main. Nothing here is left to send.
  • Fork main gets the model through the next upstream sync. The dashboard component (UI::NetWorthPace), its view hook and locale strings from #288 stay on fork main until the UI drop, as below.
  • #129 stays open: this PR was only "Part of" it, and the UI drop is still to come.
  • The branch up/net-worth-velocity is left in place. This session cannot delete branches.

Part of #129

Upstream pre-flight for the model layer of drop 10.2 (net worth velocity and momentum). Upstream issue: we-promise/sure#4079. Upstream PR: we-promise/sure#4083 (opened 2026-10-09). Same branch, same commit. Base is mirror/upstream-main; the merge base is upstream main at 275f2f994, so the diff below is what upstream will see.

Model only. The dashboard component (UI::NetWorthPace), its view hook and locale strings from #288 stay on fork main until the UI drop.


Part of #4079

Fork pre-flight: this PR (same branch, same commit). Model layer of jaysbeekay/sure#129, drop 10.2.

Summary

Adds BalanceSheet::NetWorthVelocity, a read model for how fast net worth is moving over a period (velocity, per month) and how that pace compares with the equally long period just before it (momentum). Model only: no view, controller, component or locale string.

What calls this? Nothing yet, deliberately. The dashboard figure that reads it (a small component beside the net worth headline) is a separate UI PR that follows once this one lands, per the "small, cohesive changes" guidance. Merging this changes no behaviour a user can see.

What changed

  • app/models/balance_sheet/net_worth_velocity.rb (new, 97 lines including comments)
    • velocity: (last point - first point) / days between them x 365.2425 / 12, as Money in the family currency.
    • momentum: velocity minus the velocity of prior_period, the window of the same length ending the day before period starts.
    • Both read BalanceSheet::NetWorthBreakdownSeriesBuilder#breakdown_series(period:), the series the Reports page draws, which already applies the viewer's account visibility and is cached per period.
    • Both are nil when the history of every account the series is built from (BalanceSheet::HistoricalAccountScope#account_ids) does not cover the window: each account starts at its first non-pending entry or first Balance row, whichever is earlier, and of the accounts started by the window's end the latest start must be on or before the window start. One long-lived account does not vouch for one opened inside the window; an account opened after the window ends does not hold it back, nor does one with neither entries nor balances.
    • The reference date is the period: passed in; nothing in the class reads Date.current.
  • test/models/balance_sheet/net_worth_velocity_test.rb (new, 18 tests).

Demo data

None added. Nothing renders the figures yet; the tests build their own history.

Migration notes

None. No schema change.

Verification

  • bin/rails test test/models/balance_sheet/net_worth_velocity_test.rb: 21 runs, 39 assertions, 0 failures. Three of those come from upstream review (d76ba7038): the history guard now counts Balance rows as history and ignores accounts that start after a window ends.
  • Full suite bin/rails test on d76ba7038 (upstream main at 275f2f994): 11371 runs, 48419 assertions, 0 failures, 0 errors, 34 skips.
  • bin/rubocop -f github on the changed files: no offences. bin/brakeman --no-pager -q: 0 warnings. No ERB or JS changed, so erb_lint and Biome have nothing to check.
  • Each behaviour was broken on purpose and the named test failed:
MutationCaught by
history check always passes5 tests, incl. velocity is withheld when history starts inside the period, momentum is withheld when history starts inside the prior period, there is no velocity when the family has no history
earliest instead of latest account startvelocity is withheld when any account's history starts inside the period
pending transactions count as historya pending transaction dated before the history does not count as history
momentum sign reversed4 tests, incl. momentum flips sign when growth slows
prior period shifted by a daythe prior period is the same length, ending the day before this one starts, reads the period it is given, not today's date
30-day month instead of 365.2425 / 12velocity is the change over the period, per month, and 2 more

Removing either single-point guard (points.size < 2, days.positive?) on its own is not caught, because the other still holds; they are belt and braces for a one-day window, covered by a single-day period has no velocity.

Blast radius

  • Who else hits this line, and what do they pass? No existing line changes. The class calls NetWorthBreakdownSeriesBuilder.new(family, user:) and HistoricalAccountScope.new(family, user:) with the balance sheet's own family and user, exactly as BalanceSheet and the Reports page do, and Period.custom(start_date:, end_date:).
  • What else reads what I just changed the reading of? Nothing: this adds a reader and changes no reader. grep -rn "NetWorthBreakdownSeriesBuilder.new\|HistoricalAccountScope.new" app lists net_worth_series_builder.rb, net_worth_breakdown_series_builder.rb and this file; the first two are untouched.
  • Cost when a caller exists: two cached breakdown_series calls (this window and the prior one) and one grouped entries query for the history guard.