Declare how /accounts loads each provider's connections

Merged
JA
2d ago
+285-118

Summary

Provider kit Track A, A8: how /accounts loads each provider's connections, and which of them its sync preload covers, is declared on the adapters instead of written out in AccountsController.

Part of #5003

Stacked on #5084 (A7), in Track A's native stack on #5005.

Fork pre-flight: jaysbeekay/sure#693 (same commits, against a mirror of upstream main; its CI, Gatekeeper and review history live there).

What changed

  • declares_connection lists: gains accounts_index:. Its default, built per declaration, is ordered.with_attached_logo.includes(<accounts association>); 17 adapters declare where they differ; FinanceKit (whose accounts /accounts lists separately) opts out with accounts_index: nil. nil is rejected for the Bank sync lists.
  • AccountsController::INDEX_CONNECTIONS drives both the index loads (each still through visible_provider_items) and preload_latest_sync_metadata_for_index!; the 27 hand-written loads and the hand-kept preload list are gone.
  • The provider:family generator's /accounts items patch is removed: its regex (@x_items = family.x_items.ordered) stopped matching once the loads were wrapped in visible_provider_items, so a generated provider got no /accounts load at all; it now gets the default. The generator still adds the provider's sync stats map.
  • Diff: non-test +96 / −118.

No visible change.

Review fixes (2026-11-15)

  • An adapter without declares_connection (it already existed, or --skip-adapter) leaves @<key>_items unset on /accounts, but the generator added a sync stats loop and an index section that called each and any? on it, so /accounts raised for every user. Both inserts now read the items nil-safe. Test: generates into copies of the real controller and view, then runs the inserted code with the items unset (failed with undefined method 'each' for nil before); reverting either &. is caught.
  • With --skip-view the generator still added the /accounts section, which renders the item partial that --skip-view does not write. The template's declaration loads the provider's connections onto /accounts, so the page raised a missing-template error for every family once a connection existed. The section is now skipped under --skip-view, and the generator says which partial to write and where to render the items. Test: "--skip-view leaves /accounts alone and says what to add" (failed before: the section was added). Mutations caught: keeping the insert, dropping the message, inverting the condition. Full suite at the top of the stack after the restack: 16,251 runs, 0 failures (the same 8 ReleaseNotesWorkflowTest errors, no node in the test image).

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Old vs new (Rails runner on fixtures, old index block loaded as a separate class): every one of the 27 providers yields the same visible connections in the same order, for the fixture admin (17 providers with connections) and member. The queries are identical except Trading 212 and Sharesight, whose declared queries now sort by creation before visible_provider_items instead of after; that filter is a select, so order is kept (ties on equal created_at were arbitrary before and still are).
  • Fail-first: "each provider on /accounts declares the query its connections load with" (pins all 27 queries and FinanceKit's opt-out), "a connection is on /accounts, ordered with its logo and accounts, unless it opts out", "index loads each provider's connections with its declared query" and "the index sync preload covers every provider's connections" failed before the change (no accounts_index on the declarations).
  • Mutations, each caught: default without the accounts preload; default without the logo; skip visible_provider_items; preload only the first provider; Trading 212 unsorted, sorted by id, or reversed (the last two by "index lists Trading 212 connections oldest first", added after review); Fio without active; FinanceKit listed; Brex without its nested preload; no nil guard on the Bank sync lists.
  • eager_load! clean.
  • bin/rails test (full): 15,883 runs, 0 failures; 8 errors in ReleaseNotesWorkflowTest from node missing in the test image. After rebasing the stack onto upstream main (dbeebf14c), the full suite at the top of the stack (A9): 16,246 runs, 0 failures, the same 8 environment errors.
  • bin/rubocop on changed files: no offenses. bin/brakeman: no warnings.

Blast radius

  • The same 27 @<key>_items are set, each filtered by visible_provider_items; their readers (the index sections, build_sync_stats_maps, @onchain_wallet_cards, the empty-state check) are unchanged. Nothing on /accounts reads @financekit_items.
  • INDEX_CONNECTIONS is built at class load from Provider::Factory.connection_declarations, like PANEL_CONNECTIONS and ProviderConnectionStatus::PROVIDERS.
  • Sibling left for A9: the empty-state check in accounts/index.html.erb. The index sections stay hand-kept (Track B).

Summary by cubic

Declares how /accounts loads each provider's connections on the provider adapters instead of the 27 hand-written loads in AccountsController. No visible change.

  • declares_connection gains an accounts_index list, defaulting to ordered with the logo and the declared accounts association preloaded; 17 adapters declare where they differ and FinanceKit opts out with nil.
  • AccountsController::INDEX_CONNECTIONS, built from the declarations, now drives both the index loads and the sync preload; all items still pass through visible_provider_items.
  • The provider:family generator no longer patches the index action; the sync stats map and the section it still adds read the provider's items nil-safely, since an adapter without a declaration leaves them unset.
  • With --skip-view, the generator leaves /accounts alone and says which item partial to write and where to render the items, since that partial isn't written.
  • nil is rejected for the Bank sync lists, which can't be left out.

Summary by CodeRabbit

  • Improvements
    • Account connections are displayed with provider-specific ordering and associated account details, with logos shown where available.
    • Sync information on the accounts page now covers the connections displayed there.
    • Trading 212 connections are ordered from oldest to newest on the accounts page.
  • Documentation
    • Updated provider setup guidance to explain how account details are loaded on the accounts page and what to configure when skipping view generation.