Add BalanceSheet::NetWorthVelocity for net worth pace
Part of #4079
Fork pre-flight: jaysbeekay/sure#360 (same branch, same commit; its CI and review history live there). 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, 103 lines including comments)velocity: (last point - first point) / days between them x 365.2425 / 12, asMoneyin the family currency.momentum:velocityminus the velocity ofprior_period, the window of the same length ending the day beforeperiodstarts.- 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
nilwhen the history of every account the series is built from (BalanceSheet::HistoricalAccountScope#account_ids) does not cover the window. An account starts at its first non-pending entry or its firstBalancerow, whichever is earlier (the unlinked branch ofAccount#history_start_date); the series readsBalancerows, so an account with balances but no entries counts. Of the accounts that have started by the window's end, the latest start must be on or before the window's start. One long-lived account does not vouch for one opened inside the window; an account opened after the window ends reads zero throughout it and does not hold it back; accounts with neither entries nor balances are ignored. - The reference date is the
period:passed in; nothing in the class readsDate.current.
test/models/balance_sheet/net_worth_velocity_test.rb(new, 21 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.- Full suite
bin/rails testond76ba7038(upstreammainat275f2f994): 11371 runs, 48419 assertions, 0 failures, 0 errors, 34 skips. - The three tests added from review failed on
6757a5957first:velocity is withheld when an account with only balances starts inside the period,an account opened after the prior period does not withhold momentum,an account opened after a past period does not withhold its figures. bin/rubocop -f githubon 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:
| Mutation | Caught by |
|---|---|
| history check always passes | 5 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 start | velocity is withheld when any account's history starts inside the period |
| pending transactions count as history | a pending transaction dated before the history does not count as history |
account start from entries only, ignoring Balance rows | velocity is withheld when an account with only balances starts inside the period |
| no filter on accounts started after the window ends | an account opened after the prior period does not withhold momentum, an account opened after a past period does not withhold its figures |
| momentum sign reversed | 4 tests, incl. momentum flips sign when growth slows |
| prior period shifted by a day | the 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 / 12 | velocity 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:)andHistoricalAccountScope.new(family, user:)with the balance sheet's own family and user, exactly asBalanceSheetand the Reports page do, andPeriod.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" applistsnet_worth_series_builder.rb,net_worth_breakdown_series_builder.rband this file; the first two are untouched. - Cost when a caller exists: two cached
breakdown_seriescalls (this window and the prior one) and two grouped queries for the history guard, one onentriesand one onbalances.
Summary by CodeRabbit
- New Features
- Added net-worth velocity, showing the average monthly change over a selected period.
- Added net-worth momentum, comparing that change with the preceding period of equal length.
- These figures are unavailable when account history does not cover the comparison period or there is not enough date history to calculate a change.