Add the cost basis method to the account form
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 onpersisted? && 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?, becauselock_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_laterfolds into a running sync, which may have built its calculators under the old method.Account::Syncerrecords the method each run used insync.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
AccountableResourcecontroller posts through it, so a depository can't be set tofifo. 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). createdrops a submitted method. A new account has no sales to restate, so it starts onaverage, and the select appears only once the account exists. Beforee96271027,createpassed 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.
| Before | After | |
|---|---|---|
| Desktop (1400px) | ![]() | ![]() |
| Phone (390px) | ![]() | ![]() |
Blast radius
- Who else hits these lines, and what do they pass?
AccountableResource#updateserves every account type's controller, andaccounts/_formis 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_paramsis shared, so the param is permitted once and dropped unlesssupports_trades?. It has three readers.updatedrops the method unlesssupports_trades?.createdrops it, so a new account starts onaverage.rebuilt_account_from_submissionis left unchanged: it builds the unsaved account for the 422 form, which is never saved, and the select appears only on a persisted account.updateis 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_laterexpected once; the column isfifo].resubmitting the same method starts no sync[sync_laternever called, while the rest of the update still applies].an unknown method is rejected[422; the column staysaverage; 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 staysaverage].a new account starts on average whatever the form submits[one account is created, onaveragethough the form sentfifo]. It failed onfb9a00296, where the account was created onfifo.a method sent with a switch from exchange to wallet is dropped and starts no syncanda method sent with a switch from wallet to exchange is kept and starts one sync(test/controllers/cryptos_controller_test.rb). Both failed beforeb6285b79f.a cost basis method changed while the sync ran starts another sync once it completes[a realSync#perform; the stored method changes during materialization; exactly one new pending sync], with the controlan unchanged cost basis method starts no further sync(test/models/account/syncer_test.rb). The first failed beforeb6285b79f.
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):
| # | Mutation | Caught by |
|---|---|---|
| B1 | no supports_trades? guard | non-trade account test |
| B2 | sync on every update | resubmit test, non-trade test |
| B3 | no sync | change test |
| B4 | method not permitted | change test, unknown-method test |
| B5 | select on every form | select-placement test |
| B6 | saved_change_to_cost_basis_method? instead of the comparison | change test |
| B7 | create keeps the method (the code at fb9a00296) | new-account test |
| B8 | no post-update supports_trades? check | exchange-to-wallet test, depository test |
| B9 | no restatement after a raced sync | raced-sync test |
| B10 | restatement on every sync | unchanged-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-secondarytoken. 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.



