Add provider kit base classes and move Brex onto them
Summary
Provider kit Track B, B1: abstract base classes for a provider's connection and accounts, a shared syncer and unlinking, with Brex moved onto them.
Part of jaysbeekay/sure#670. Track B now stacks on top of Track A: this PR's base is A9 (up/provider-kit-empty-state, jaysbeekay/sure#694) and it is the next PR in fork stack #687.
Upstream draft PR: https://github.com/we-promise/sure/pull/5006 (same branch, same commit), in native stack we-promise/sure#5268 on refactor/provider-kit.
What changed
ProviderItemandProviderAccount: abstract base classes (self.abstract_class = true) over the existing per-provider tables. No migration, no STI;Sync#syncable_typeandAccountProvider#provider_typekeep recordingBrexItem/BrexAccount.ProviderItemincludesSyncableand the shared unlinking, and exposesprovider_keyandprovider_accounts(read from the adapter'sdeclares_connectionsince7514143f2).ProviderAccountholds theaccount_provider/accountassociations andcurrent_account.
ProviderItem::Synceris Brex's syncer generalised by provider key: i18n scope<key>_items.syncer, import viaimport_latest_<key>_data, Sentry tag<key>_item_id, stats source<key>. A subclass names its credential errors in#credentials_error?.- A failed sync also writes one
DebugLogEntryrow (2066140ef): categoryprovider_sync_error, the provider key, the family, and<key>_item_id, error class and message in its metadata. This answers the Gatekeeper's debug-log gap on jaysbeekay/sure#726. BrexItem::Syncerstays as a thin subclass, so the constant andBrexItem::Syncer::SafeSyncErrorstill resolve and Brex's existing tests pass unedited.ProviderItem::Unlinkingis Brex's unlinking concern, keyed by the provider account's class.brex_item/unlinking.rbis removed.test/support/provider_item_lifecycle_tests.rb: a shared contract (sync, unlink incl. another provider's link surviving, dry-run unlink, destroy) that each provider on the kit includes; Brex is the first.
Nothing user-visible changes for a family. A super admin now sees a failed sync in Settings > Debug (Settings::DebugsController is admin-only). One log line reads "sync failed for BrexItem 1" where it read "sync failed for Brex item 1".
Not changed on purpose: the other 24 <provider>_item/unlinking.rb copies and their syncers move one provider per later PR. The holding update_all in unlinking is redundant with AccountProvider has_many :holdings, dependent: :nullify, but kept so this PR moves code without changing it.
Demo data
No demo data needed: no new records or UI.
Migration notes
No migration.
Verification
- Existing tests: no file under
test/changed except the three new ones; all Brex model, syncer, account-flow, importer and controller tests pass. - Mutations, each caught: unlinking with the wrong provider type; unlinking every link; a dry run that removes; credential errors never matched; the wrong i18n scope;
ProviderAccountwithoutdependent: :destroy; the base not abstract. One survives by design: skipping the holdingupdate_all(see above). bin/rails test(full): 15,866 runs, 0 failures; 8 errors inReleaseNotesWorkflowTestfromnodemissing in the test image.bin/rubocopon changed files: no offenses.bin/brakeman: no warnings. No ERB or JS changed.- Diff with copy detection (
git diff -C): non-test +92 / -191.
Rebased onto A9 (2026-11-15) and one commit added, 7514143f2: the provider key and accounts association are now read from the adapter's declares_connection (Track A's A1) instead of derived from the model name, so a connection with no declaration raises NotImplementedError rather than guessing. This also answers the light review's "provider_key is name-fragile" nit. Brex's declared brex / :brex_accounts equal the derived values, so nothing changes for Brex.
- Tests (fail-first): "a provider keys its connection by its adapter's declaration" (a stubbed declaration with another key and association; failed before with
"brex"), and "a connection without a declaration has no provider key" (failed before: nothing raised). - Mutations, each caught: deriving the key from the model name again; deriving the association from the key again; returning nil instead of raising.
- Existing tests:
test/diff against A9 is still only the three new files. bin/rails test(full, at7514143f2): 16,260 runs, 0 failures; the same 8ReleaseNotesWorkflowTesterrors (nonode). Rubocop clean.
Full review fixes (2026-11-17), b17701f8f: two tests pin what the syncer passes by provider key, which no test checked before.
- "counts only the linked account's Brex-sourced transactions": a linked account with one Brex entry and one manual entry;
tx_importedandtx_seenare 1. With the source mutated to"BREX":Expected: 1, Actual: 0. - "tags the Sentry report with the Brex item id": expects
set_tags(brex_item_id:). With the tag key mutated tobrex_id:unexpected invocation: #<Mock:scope>.set_tags(brex_id: ...). - Existing tests:
brex_item/syncer_test.rbgains these two; no existing test changed. - Follow-up for the review's residual risk: jaysbeekay/sure#834 (cache the
connection_declarationlookup).
Blast radius
BrexItem::Synceris built byBrexItem::Provided#syncer(brex_item/provided.rb:13), which is public and overridesSyncable's private#syncer.Syncable#perform_syncand#perform_post_synccall it fromSync#perform;BrexItemTest"syncer returns BrexItem::Syncer instance" calls it directly. Both paths build the same subclass.BrexItem#unlink_all!has one caller,BrexItemsController#destroy.Family::Syncer.syncable_item_associationschecksincluded_modules.include?(Syncable); an inheritedSyncablestill counts, and the contract asserts it.- Polymorphic types (
syncable_type,provider_type) are unchanged because the parents are abstract; the contract asserts the link's type.