Add the cost basis method to the account form

Merged
JA
1mo ago
+153-7

Part of jaysbeekay/sure#551. This is PR 2b of the stack (PR 2 #580 ← PR 2b ← 3 ← 4 ← 5; owner decision 13). Its base is main, because #580 merged as 9c61eaf27; main was merged down into this branch (249539faf), not rebased.

Problem

#580 adds accounts.cost_basis_method {average, fifo}, but nothing in the app can change it, so FIFO can only be switched on from the console. A change also has to restate the stored figures, and nothing starts that.

What this changes

  • A "Cost basis method" select (Average cost / First in, first out (FIFO)) on the edit form of an account that supports trades, which covers investment and crypto-exchange accounts. It sits in the shared accounts/_form, gated on persisted? && supports_trades? the way the category-matcher toggle is gated on its own check. A hint explains that a change recalculates past realized gains and the average cost held, and that a sale of shares whose cost is unknown shows its gain as unknown. All strings are i18n (accounts.form.*, English; the other locales fall back to it).
  • Changing the method starts a sync of the account, which restates the stored figures under the new method. Resubmitting the same method starts none. The change is detected by comparing against the value before the update, not with saved_change_to_cost_basis_method?, because lock_saved_attributes! saves again and clears that. Mutation B6 below confirms this.
  • A change made while a sync is already running is restated afterwards. sync_later folds into a running sync, which may have built its calculators under the old method. Account::Syncer records the method each run used in sync.data, and post-sync, which runs after the sync completes, starts a new sync if the stored method has changed since (CodeRabbit, b6285b79f).
  • The shared update path drops a submitted method for an account without trades. Every AccountableResource controller posts through it, so a depository can't be set to fifo. The method is applied after the other fields, only where the account then trades, so a crypto account switched between exchange and wallet in the same submission is judged by its new subtype (CodeRabbit, b6285b79f).
  • create drops a submitted method. A new account has no sales to restate, so it starts on average, and the select appears only once the account exists. Before e96271027, create passed the method through (light review 6380945413).

Screenshots

Edit investment account. The before shots are from #580's head; the after shots are from this PR. Both were rendered headless by a throwaway system test on the test containers, with fixture data.

BeforeAfter
Desktop (1400px)beforeafter
Phone (390px)before phoneafter phone

Blast radius

  • Who else hits these lines, and what do they pass? AccountableResource#update serves every account type's controller, and accounts/_form is rendered by every type's form. A non-trade account never shows the select, and its update drops the param, so its update is unchanged. That is tested on a depository.
  • Siblings: account_params is shared, so the param is permitted once and dropped unless supports_trades?. It has three readers. update drops the method unless supports_trades?. create drops it, so a new account starts on average. rebuilt_account_from_submission is left unchanged: it builds the unsaved account for the 422 form, which is never saved, and the select appears only on a persisted account. update is the only path that changes the method.

Tests

In test/controllers/investments_controller_test.rb; the bracket says what each assertion catches.

  • changing the method starts one sync of the account [sync_later expected once; the column is fifo].
  • resubmitting the same method starts no sync [sync_later never called, while the rest of the update still applies].
  • an unknown method is rejected [422; the column stays average; no sync].
  • the method select is on the form of an account that supports trades, and only there [2 options on the investment form, 0 selects on a depository's form].
  • an account that does not support trades ignores a submitted method [a depository stays average].
  • a new account starts on average whatever the form submits [one account is created, on average though the form sent fifo]. It failed on fb9a00296, where the account was created on fifo.
  • a method sent with a switch from exchange to wallet is dropped and starts no sync and a method sent with a switch from wallet to exchange is kept and starts one sync (test/controllers/cryptos_controller_test.rb). Both failed before b6285b79f.
  • a cost basis method changed while the sync ran starts another sync once it completes [a real Sync#perform; the stored method changes during materialization; exactly one new pending sync], with the control an unchanged cost basis method starts no further sync (test/models/account/syncer_test.rb). The first failed before b6285b79f.

Sync is asserted with a mocha expectation, not a Sync count. sync_later folds into a visible sync, so a count can stay flat while the call happened.

Evidence

Fail first. On #580's head the three positive tests fail as expected. With no select, 0 options are found. The PATCH with lifo redirects (302) instead of returning 422, because the param isn't permitted. fifo isn't saved. The two negative tests pass there, as they should; the mutations below show they bite.

Mutations (10, all caught):

#MutationCaught by
B1no supports_trades? guardnon-trade account test
B2sync on every updateresubmit test, non-trade test
B3no syncchange test
B4method not permittedchange test, unknown-method test
B5select on every formselect-placement test
B6saved_change_to_cost_basis_method? instead of the comparisonchange test
B7create keeps the method (the code at fb9a00296)new-account test
B8no post-update supports_trades? checkexchange-to-wallet test, depository test
B9no restatement after a raced syncraced-sync test
B10restatement on every syncunchanged-method control

Suite at b6285b79f: 14377 runs, 66799 assertions, 0 failures, 0 errors, 38 skips. test/system/accounts_test.rb (headless Chromium): 9 runs, 0 failures. bin/rubocop, erb_lint and bin/brakeman are all clean.

AGENTS.md

I checked the change against the current we-promise/sure AGENTS.md:

  • Controller: thin; a param guard and a sync call.
  • Design system: the select is the styled form builder's own, and the hint uses the text-secondary token. There are no raw palette classes and no new styles.
  • Copy: the strings go through t().
  • Screenshots: included above.
  • Commit: the subject is ≤72 characters.
  • Migrations, API: none.

Summary by CodeRabbit

  • New Features
    • Existing accounts that support trades can now choose between Average cost and FIFO.
    • Changing an account’s cost-basis method triggers an account sync; saving the same method does not.
    • New accounts continue to use Average cost, and accounts that do not support trades are unaffected by submitted cost-basis choices.