fix(accounts): subtype dropped on create when assigned before accountable_type#2432
fix(accounts): subtype dropped on create when assigned before accountable_type#2432vlnd0 wants to merge 1 commit into
Conversation
…_type we-promise#2356 build the accountable inside Account#subtype= so a top-level subtype survives create. That fix assumes accountable_type is already set when subtype= runs, but the real controller path violates it: strong-params permit preserves filter order, and account_params lists :subtype before :accountable_type. So on create subtype= runs while accountable_type (and accountable_class) is still blank, the build is skipped, and the chosen subtype is silently dropped — the account renders with the type's fallback label (e.g. a Depository shows 'Cash' instead of 'Savings'). The existing regression test only covered the accountable_type-first order, so it never caught this. Make the writer order-independent: when subtype arrives before the type, stash it and apply it from an accountable_type= override once the type is known. Add regression tests for the permit order and for create_and_sync.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesDeferred subtype assignment in Account model
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
|
@jjmata if all ok, i suggest to merge |
Problem
Creating an account and choosing a subtype (e.g. a Cash/Depository account set to Savings) silently drops the subtype on create — the account renders with the type's fallback label (
Depository.display_name→ "Cash") instead of the chosen "Savings". Editing an existing account works; only create is affected.Cause
#2356 made
Account#subtype=build the accountable so a top-levelsubtypesurvives create:This assumes
accountable_typeis already set whensubtype=runs. The real controller path violates that assumption:account_paramspermits:name, :balance, :subtype, :currency, :accountable_type, …—:subtypebefore:accountable_type.ActionController::Parameters#permitbuilds its result by iterating the filter list in order, so the permitted hash carriessubtypebeforeaccountable_type.subtype=whileaccountable_type(and thusaccountable_class) is still blank → the build is skipped →accountable&.subtype=is a no-op → subtype dropped.The existing regression test set
accountable_typefirst, so it exercised the working order and never caught this.Fix
Make the writer order-independent. When
subtypearrives before the type is known, stash it and apply it from anaccountable_type=override once the type — and thereforeaccountable_class— is available.Tests
Added regression coverage for:
subtypeassigned beforeaccountable_type(the permit order).create_and_syncwith attributes in permit order.Both fail on
mainand pass with this change. Existing subtype tests still pass.Summary by CodeRabbit