Offer offset accounts on the new-loan form

Draft
JA
2mo ago
+230-9

Summary

Closes #325.

The new-loan form's offset select was always empty. LoanOffsetAccount.eligible_accounts_for returned Account.none for a loan with no account, and on new and create the loan is a bare Loan.new.

It now judges a new loan's candidates on the account create will build: in Current.family, in the submitted currency (the family's currency by default), owned by the viewer, and visible to the whole family when the family shares by default (Loan.viewers_of). Each candidate still has to pass LoanOffsetAccount#valid?, so the list never offers an account the save refuses. set_offset_accounts runs before create, so a failed create, which re-renders new without re-running callbacks, lists the offsets too.

Since the Gatekeeper light review (ee48897): new offsets come only from visible accounts, as the collateral list's do, so an account pending deletion or one the user disabled is no longer offered, on either form. An account the loan is already linked to stays listed whatever its status, because the select submits only what it lists and leaving a link off it would unlink it on the next save. That required #338's fix as well: a fresh record for an existing link fails the uniqueness check, so on main the edit form never listed a linked offset at all (shown below). #338's three lines are carried verbatim.

Scope and dependencies

  • Issue: #325
  • Parent PR/branch, if stacked: none. Base main.
  • Related: #338 (@duquerson, #329) changes the .select inside the same method so a linked offset is judged against its existing join row. #338 merged into main on 2026-10-12 as 333e242a3, and this branch merged it in 484eb1f11 (see Validation). The integration branch carries both.
  • Intentionally out of scope: validation, link storage, the form markup, #338's other changes (its controller, form and model tests). The save path still does not check account status; the list is narrower than the save, which keeps "never offers an account the save refuses".

Blast radius. eligible_accounts_for has one caller, LoansController#set_offset_accounts, on new, create, edit and update. Since ee48897 the edit form changes too: it no longer offers accounts pending deletion or disabled as new offsets, and it now lists the loan's existing links, which it did not before.

Sibling. Loan.collateral_candidates_for already takes family: and currency:, applies visible, and LoansController#set_collateral_candidates keeps the current link; this follows its shape. Unchanged. No other form builds a candidate list from accessible_by.

Evidence checklist

  • Each issue in this PR has its own acceptance criteria and no unrelated issue work is included.
  • Contract rows touched by this change name existing tests.
  • Any gate test has an observed failing mutation transcript attached.
  • Fixtures used as evidence assert internal consistency.
  • Financial calculations use BigDecimal and documented rounding points. (No calculation changed.)
  • De-identification uses token-shape allowlists; no personal data is included.
  • Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.

Tests (test/controllers/loans_controller_test.rb), each run red on main first:

TestOn main
the new-loan form lists a same-currency asset shared with the familyfails: option absent
the new-loan form lists the owner's private asset when the family does not share by defaultfails
the new-loan form lists offsets in the submitted currencyfails
a failed create re-renders with the offsets listedfails
every account the new-loan form offers is one the save acceptsfails
the new-loan form leaves out what the save would refuse (private asset in a share-by-default family, other currency, liability, other family)passes trivially on main (empty list); load-bearing after the fix, see M2
the edit form lists the same offsets as beforepasses on main and after
the new-loan form leaves out an account that is pending deletion or disabled (a visible control beside them)fails before ee48897: the pending-deletion account is listed
the edit form keeps listing an offset the loan already has once it is disabledfails before ee48897; fails on main too with the account still visible, since the uniqueness check drops every existing link

Mutations, each restored after:

MutationCaught by
M1: controller passes currency: nil"lists offsets in the submitted currency"
M2: drop the valid? filter"leaves out what the save would refuse"
M3: build the prospective account with owner: nilnot caught, by construction: Account.accessible_by(viewer) already restricts candidates to what the viewer can see, so the owner adds nothing to the visibility rule. Kept so the probe account matches what create builds.
M4: drop visible"leaves out an account that is pending deletion or disabled"
M5: drop the existing-link union"keeps listing an offset the loan already has"
M6: judge an existing link with a fresh record"keeps listing an offset the loan already has"

The probe account is built with Account.new(family_id:) rather than family.accounts.build, so the unsaved account never enters the family's loaded association.

Validation

  • bin/rails test test/controllers/loans_controller_test.rb test/models/loan_offset_account_test.rb test/models/loan_test.rb test/models/loan/ test/models/account_test.rb: 406 runs, 0 failures.
  • Full bin/rails test: 12,369 runs, 4 failures, 1 error, all outside loans and all environmental. The four Anthropic settings/registry tests read ANTHROPIC_BASE_URL, which this container sets; they pass with it unset. Provider::OpenaiTest#test_extra_headers_stringifies_nested_values_Ruby-style expects Ruby 3.4's Hash#inspect, and this container could only run Ruby 3.3.6 (the 3.4.9 source download is blocked by the egress policy). CI runs the pinned Ruby.
  • bin/rubocop on the changed files: no offenses.
  • After ee48897: offset, loans controller, accounts controller, loan model and test/models/loan/ suites, 467 runs, 0 failures. Merged into the integration branch (#404, 351a79693) beside #338: 272 runs across the offset, loans, accounts and loan suites, 0 failures.
  • System tests: not run locally (no chromedriver in this container); none touch the new-loan offset select.
  • Checked against upstream AGENTS.md (read from mirror/upstream-main).

Merged main after #338 landed (2026-10-12, 484eb1f11). #338 merged into main as 333e242a3 and rewrote the same two methods as this PR, so both conflicted:

  • LoanOffsetAccount.eligible_accounts_for keeps this branch's version, which already carries #338's existing-link rule.
  • LoansController#set_offset_accounts combines #338's rule (keep the loan's own links only when the form submitted no offset ids) with this branch's family and currency for a new loan.
  • One defect from combining them, fixed here. #338 reads that flag with params.dig(:account, :accountable_attributes). This PR's test that GETs the new-loan form with account as a string then raised TypeError: String does not have #dig method; this was observed on the merge before the fix. submitted_accountable_attributes now returns the attributes only when both levels are hashes, as the integration base's e4e8a002e already does. Its second test, with the attributes as a string, comes with it.
  • Mutations, each caught: dropping the inner hash check, and dropping the outer one.
  • Results: the loans controller, offset and loan model suites give 144 runs, 0 failures. Full bin/rails test gives 12,385 runs, 1 failure: the known Provider::OpenaiTest Ruby 3.3 vs 3.4 string. bin/rubocop on the three files: no offenses.
  • The controller and model now match the integration base, apart from #402's attributes, which are not part of this PR.

Review findings

  • Gatekeeper light review (comment 6094691716, parts 6094691823 and 6094691942), MODERATE, the offset list does not apply visible as its collateral sibling does: valid, fixed in ee48897. OffsetResolver does not filter by status either, so a linked account pending deletion would keep reducing interest until it went. Narrowing alone would have unlinked a disabled offset on the next save, so existing links stay listed; checking that exposed the uniqueness defect #338 fixes, carried here. Tests and mutations M4–M6 above.

  • Same review, coordinate ordering with #329/#338: both now carry the same existing-link check, so the second to merge takes this PR's version of eligible_accounts_for. #338 is not a draft and is not this session's PR; it is untouched.

  • "code master" review (comment 6106070862), hold, on ee48897: F1 and F2 are confirmed, and both are fixed in 1f0ad1434, pushed on the owner's instruction (2026-10-11: "Push the held commits; go with your recommendations").

    • F1, a currency sent as a list: confirmed. account[currency][]=USD&account[currency][]=EUR reached where(currency:) as IN, so the lists stopped matching what the save accepts.
    • F2, a scalar account param: confirmed. GET /loans/new?account=USD raised TypeError: String does not have #dig method, a 500.
    • Both paths were already on main in the collateral list (set_collateral_candidates). This PR added the same read for offsets.
    • Fix: submitted_currency takes the value only when account is a hash and the currency is a string; otherwise both lists render exactly as on a first render.
      • New tests: "the new-loan form reads a currency submitted as a list as no currency" and "the new-loan form renders when the account param is not a hash". Both fail without the helper (a wrong list, and the TypeError).
      • loans_controller_test.rb: 51 runs, 0 failures.
      • Left alone: the dig in protect_inaccessible_collateral, which runs only on update. A scalar account there still raises, but so would the strong parameters that read it next, so guarding one of the two wouldn't change the response.
    • F3, owning_account vs account: declined. Loan#owning_account returns @owning_account || account (loan.rb:170-172). It is account for every persisted loan, and the unsaved account for a new one.
    • F4, a validation per candidate on a wider set: not worth its code. The set widens only by accounts already linked to this loan, which are few by construction.
    • F5, the .or is fragile if visible changes: declined. Rails raises ArgumentError ("Relation passed to #or must be structurally compatible") when the two sides diverge, so a future change to visible fails loudly rather than generating wrong SQL. The behaviour tests here would also fail.
    • F6, owner: viewer is not load-bearing: already recorded. M3 above says so. It is kept so the prospective account matches what the save builds.
  • Nightly review (comment 6134303852), READY TO MERGE, on 484eb1f11. No blocking or other material findings. Nothing to change.

Review responses are recorded here rather than as comments, because comments posted from this session get an assistant footer appended, which this repository's attribution rule forbids.

Risks, blockers, and follow-up

  • Merge order with #338: settled. #338 is on main, and 484eb1f11 resolved both conflicts (see Validation).
  • Next unblocked: none from this PR.