Judge a new loan's offset accounts on the account it is created on
Requested by Jonathan · project thread
Summary
Closes #290
Before: creating a variable-rate loan with offset_account_ids accepted an offset in another currency, and one private to its owner in a family that shares new accounts by default. An edit that changed the loan's currency and its offsets together was judged on the old currency.
After: the same rules LoanOffsetAccount applies to an existing loan apply on creation and are judged on the currency as submitted. Both cases are refused with the existing messages ("must use the same currency as the loan", "must be visible to every loan viewer").
How: LoanOffsetAccount#loan_account reads loan.owning_account (added by #289, which Account sets before validating) instead of loan.account, which is nil for a loan being created and the stored row on an edit. The viewer rule moves from Loan#collateral_viewers to Loan.viewers_of, so offsets and collateral share one rule: a new account in a family that shares by default counts everyone in the family as a viewer. The link reaches the loan through the has_many inverse, so both the validation and the after_save link creation see the same instance Account handed itself to. No explicit hand-over was needed; one was tried and every mutation showed it redundant.
Scope and dependencies
- Issue: #290 (Gatekeeper APPROVED, comment 5958297588)
- Parent PR/branch, if stacked: none; builds on #289, merged.
- Intentionally out of scope:
LoanOffsetAccount.eligible_accounts_for, the form's candidate list, still offers nothing onnew. Recorded as #325 (enhancement), as the light-touch Gatekeeper review asked.- A separate defect found while testing, raised as #319, not fixed here. An edit submitted through the account that changes only the offsets is silently dropped: the loan has no column changes, so
belongs_toautosave neither validates nor saves it, andsync_offset_accountsnever runs. Reproduced onmainwithPATCH /loans/:idsubmitting onlyoffset_account_idson a variable loan: 302 with the success notice, no link created. It also means a currency change plus offsets with no other loan field changed is not validated at all. The edit tests here therefore change a loan column too.
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:
LoanOffsetAccountTest(existing cases unchanged and green),Loan::CollateralAccountTest(the collateral viewer rule now delegates). - Any gate test has an observed failing mutation transcript attached (below).
- Fixtures used as evidence assert internal consistency: each test asserts its precondition (
share_all_by_default?, no shares on the offset, currency differs, rate type). - Financial calculations use
BigDecimal: n/a, no calculation changed. - De-identification: n/a, no personal data.
- Files changed, decisions, tests/runtime, risks, and next unblocked issue are documented below.
Files: app/models/loan.rb, app/models/loan_offset_account.rb, test/models/loan_offset_account_test.rb, test/controllers/loans_controller_test.rb.
Validation
New tests, run against unmodified main first:
| Test | main | this PR |
|---|---|---|
| model: a new loan links an offset in its currency that the family can see | pass | pass |
| model: a new loan refuses an offset in another currency | fail (nothing raised) | pass |
| model: a new loan the family will share refuses a private offset | fail (nothing raised) | pass |
| model: a family that does not share by default only needs the owner to see the offset | pass | pass |
| model: an edit that changes the currency is judged on the new currency | fail (saved) | pass |
| model: ...for a kept offset (existing join row) | fail (saved) | pass |
| model: an edit in the loan's own currency still keeps its offset | pass | pass |
| controller: refuses a new loan whose offset is in another currency | fail | pass (422, no account, no link) |
| controller: refuses a new loan whose offset the rest of the family cannot see | fail | pass (422) |
Mutations, each run against test/models/loan_offset_account_test.rb:
loan_accountback toloan&.account: 4 failures (both create negatives, both edit negatives).Loan.viewers_ofwithout the share-by-default rule for a new account: 1 failure (the private-offset case).
Changed existing test: LoansControllerTest "creates a variable loan with visible offset accounts" used an offset visible only to its owner in a family that shares by default, so it asserted the defect. The offset is now shared with the family, and the created loan is found by type so a failed create cannot pass the lookup on the offset account.
Commands:
bin/rails test: 11,766 runs, 4 failures and 1 error, all inProvider::OpenaiTest,Provider::RegistryTestandSettings::HostingsControllerTest. The same 5 fail onmainin this sandbox (Anthropic/OpenAI environment variables are set here); none touches loans.- After merging
main(f066f5ac1): loan model, loan controller andAccountTestsuites, 316 runs, 0 failures. CI 32/32 green on that head. bin/rubocopon the changed files: no offenses.bin/brakeman --no-pager: 0 warnings.- erb_lint and Biome: no ERB or JS changed.
- Checked against
AGENTS.mdas carried on forkmain(last synced 2026-09-18); upstream's current copy could not be read from this session.
Risks, blockers, and follow-up
Loan.viewers_ofnow feedsLoanOffsetAccount.eligible_accounts_foronedit/update; for a persisted account it is the sameshared_with?filter as before.- Follow-up: #319 (dropped offset-only edit), #325 (offset list on the new-loan form).
- Next unblocked: #188 / #191 is the other open loan item.
Summary by CodeRabbit
- Bug Fixes
- Loan offset accounts are now checked against the loan’s currency and visibility rules during creation and edits. Invalid links are rejected without creating the loan or offset link.
- Family-visible offsets can be linked when shared with all loan viewers, and private offsets remain supported when the family’s default sharing setting is private.