Select and link Lunch Flow accounts through the provider kit's flow
Summary
Lunch Flow controller convergence, C1e: selecting and linking. Lunch Flow's preload, select and link actions now run through the provider kit's account flow (LunchflowItem::AccountFlow < ProviderItem::AccountFlow) instead of their own 370 lines, so they behave exactly as Brex's do. That fixes three bugs: linking crashed (a 500) when there was no connection, an empty account list crashed linking, and with several connections Lunch Flow silently used whichever one the database returned first. The user now chooses the connection, as decided for the kit.
Part of #5003 (Track B, controller-layer convergence, option (b) and the several-connections decision, decided 2026-11-16). Selection and linking are one slice because the selection forms now post a connection id that only the new link code reads; split, the first slice would leave multi-connection linking broken.
Stacked on #5244. Fork pre-flight: jaysbeekay/sure#734 (same branch, same commit; its CI, Gatekeeper review and screenshot history live there).
Behaviour delta (Lunch Flow)
| Before | After | |
|---|---|---|
| Several connections with API keys | the first by primary key, silently; the account list from one connection's key could be linked under another | the user chooses: the new-account page lists "Lunch Flow - " per connection; without a choice, preload returns select_connection and select/link redirect to Provider Settings with "Choose a Lunch Flow connection…" |
| One connection | the new-account page shows "Lunch Flow" | unchanged (still one "Lunch Flow" entry, no id) |
| Preloading accounts on the new-account page | always, for the one entry | still, with one connection; with several there is no preload (the entries are per connection, as Brex's are) |
| Account list | read from the family's first key | read from the chosen connection's key and its 5-minute cache; linking reads the cache too instead of calling the API again |
| Cached empty list | refetched | served from the cache (cached: true, has_accounts: false) |
link_accounts / link_existing_account with no connection | first_or_create! raised RecordInvalid (500) | redirect to Provider Settings, nothing created |
The provider lists no accounts (accounts: nil) | NoMethodError (500) | "Failed to link accounts" |
| An account type Lunch Flow doesn't support (crafted URL or POST) | listed and linked anyway | select: "No accounts found…"; link, and selecting or linking an existing account of that type: "Unsupported Lunch Flow account type", nothing linked (the existing-account guard is the kit's, from the flow-linking branch) |
An unknown account_id (existing account) | 404 | redirect to /accounts, "No account specified" |
| Existing-account selection when the provider lists nothing | "No Lunch Flow accounts found…" | "All Lunch Flow accounts are already linked" (the kit's message) |
| Linking several accounts | each saved as it went | one transaction (the kit's link_new_accounts!), meant so a failure partway links none. No test here forces a failure partway, so treat that as the kit's design rather than a pinned result. An unexpected error shows the translated generic message |
| Preload's unexpected-error message | the raw exception text | lunchflow_items.errors.unexpected_error |
| Currency of a newly linked account | the provider account's after the snapshot upsert (payload, else stored, else USD) | unchanged: the kit now prefers that same value |
What changed
LunchflowItem::AccountFlow: the kit's hooks for Lunch Flow (API error class, connection resolver, fetching accounts with the chosen connection's key, upserting a provider account, cache-sensitive paramsapi_key/base_url).LunchflowItem:with_credentialsscope andresolve_for, as Brex's.LunchflowAccount:name_for,currency_for,default_accountable_attributes(none, as before).LunchflowItemsController: preload, select, select-existing and both link actions call the flow; the old bodies go (-370 lines).ProviderItem::AccountFlow#link_new_accounts!: currency is the provider account's after the upsert, falling back to the payload's. For Brex both come from the same payload (BrexAccount.currency_code_from_money), so Brex is unchanged; Brex's tests pass unedited.Provider::LunchflowAdapter.connection_configs: one entry per credentialed connection when there are several.- Views: both selection forms post
lunchflow_item_id. - Locales (17):
no_api_key→no_api_tokenandlunchflow_account_not_found/lunchflow_account_already_linked→provider_account_*, translations kept;unexpected_errorunder both selection scopes copies each locale's existingapi_error.unexpected_error; the touched blocks are sorted. New English keys for the connection choice, the type check and the per-connection entries; other locales fall back to English for those. - Removed, now unused:
Family#has_lunchflow_credentials?,Provider::LunchflowAdapter.build_provider,select_existing_account.no_accounts_found.
Screenshots
The new-account method selector for a depository account ("How would you like to add it?"), captured headlessly in Chrome at 900 px from the fixture family: before at the base head (7ec62e78), after at this PR's head (e25c3eb4).
Two Lunch Flow connections ("Everyday banking" and "Joint account"). Before, one "Link with Lunch Flow" entry, which linked from whichever connection the database returned first. After, one entry per connection.
| Before | After |
|---|---|
![]() | ![]() |
One Lunch Flow connection: unchanged, a single "Link with Lunch Flow" entry.
| Before | After |
|---|---|
![]() | ![]() |
The selection dialogs only gain a hidden lunchflow_item_id field, so they look the same.
Demo data
No demo data needed.
Migration notes
No migration.
Verification
- Tests (fail-first): new
LunchflowItemsSelectionTest(20 tests), one per row of the table: choosing among two connections (preload, select, link); the chosen connection's accounts offered and linked under it, with its id in the form; the existing-account form's id; no connection; nil list; unsupported type (select and link); unknown account (select and link); empty list for an existing account; translated preload error; cached empty list; per-connection and single entries on the new-account page; the cache read on linking. On the old code 16 fail. Four pass before and after as guards: the stored-currency case, "account not found", the single entry, and the English no-credentials text. The second connection's id sorts after the fixture's, so code that takes the family's first connection picks the wrong one. - Existing tests: five
LunchflowItemsControllerTesttests stubbedProvider::LunchflowAdapter.build_provider, which no longer exists; they now stub the connection'slunchflow_providerinstead (stub lines only, assertions unedited, including the German error texts). Brex's tests pass unedited. - Mutations, each caught: the chosen id ignored; several connections resolved to the first silently; accounts fetched with the family's first key; the provider account upserted under the family's first connection; the kit reverting to the payload's currency; one entry regardless of connections; either form dropping the connection id.
bin/rails test(full, at the C1e commit): 16,371 runs, 0 failures; the same 8ReleaseNotesWorkflowTesterrors (nonodein the test image).bin/rubocop,erb_linton the changed views: no offenses.bin/brakeman: no warnings. Every locale file parses.- Size: non-test +259 / -509, of which locales +121 / -106 (mostly renames, alphabetical moves).
- Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.
Blast radius
Provider::LunchflowAdapter.connection_configsis read by the new-account method selector (accounts/new), which gates the preload controller on the"lunchflow"key; that is why the single-connection entry keeps that key.ProviderItem::AccountFlow#link_new_accounts!also serves Brex (see above).link_existing_account!, setup, update, create, destroy and sync are unchanged here; setup converges in C1f.- Siblings: Mercury converges in its own slices.
Review notes
A code review of the diff before commit raised points across two passes. Taken: linking moved into this slice (the selection forms posted a connection id the old link code ignored); orphans removed (build_provider, has_lunchflow_credentials?, an unused locale key); method spacing and placement; touched locale blocks sorted. Noted, not changed:
- no preload with several connections, the existing-account empty message, the transaction, and the type check: the kit's behaviour, in the table above;
updatestill expires the cache with its own key: unchanged behaviour, replaced when the controller moves onto the kit;- the kit re-queries the credentialed connections a few times per request: the kit's existing behaviour.
Summary by cubic
Lunch Flow's account selection and linking now run through the provider kit's account flow (LunchflowItem::AccountFlow) instead of their own 370 lines, so they match Brex's behavior. This fixes three bugs: linking raised a 500 without a connection, a nil account list crashed linking, and with several connections Lunch Flow silently used the database's first one.
Behavior changes
- With several credentialed connections the new-account page lists "Lunch Flow - " per connection; preload and selection ask which to use, and both link forms post
lunchflow_item_id. - Accounts come from the chosen connection's key and its 5-minute cache, and a cached empty list is now served from cache.
- Unsupported account types are refused; an unknown existing-account id redirects to /accounts, and linking without a connection redirects to Provider Settings.
- Linking several accounts runs in one transaction, and preload errors show a translated generic message instead of raw exception text.
What changed
LunchflowItem::AccountFlowadds the kit hooks (error class, connection resolution, fetch, snapshot upsert); the controller's preload/select/link bodies are deleted.Provider::LunchflowAdapter.connection_configsemits one entry per credentialed connection when several exist.- The kit's
link_new_accounts!now prefers the snapshot-upsert currency, so a stored currency is kept when the payload omits one (Brex unchanged). - Locale keys renamed (
no_api_key→no_api_token,lunchflow_account_*→provider_account_*); removed now-unusedFamily#has_lunchflow_credentials?andProvider::LunchflowAdapter.build_provider. - The controller's cache-write guard goes with the code it guarded; the kit's flow guards the cache write the same way, and the helper's cache test is dropped.
- New
LunchflowItemsSelectionTest(20 tests) pins the connection-choice, missing-connection, unsupported-type and cache cases; controller tests now stublunchflow_providerinstead ofbuild_provider.



