From fea283488c67aa7affbe40cc58df0b940ef419df Mon Sep 17 00:00:00 2001 From: jaysbeekay Date: Wed, 2 Dec 2026 01:44:16 +0100 Subject: [PATCH] Derive panel syncable types from connection declarations (#5079) ## Summary Provider kit Track A, A2: `Settings::ProvidersController::PANEL_SYNCABLE_TYPES` is derived from the adapters' connection declarations instead of hand-kept. Part of #5003 Stacked on #5005 (A1), in Track A's native stack on #5005. Fork pre-flight: jaysbeekay/sure#686 (same commits, against a mirror of upstream `main`; its CI, Gatekeeper and review history live there). ## What changed - `PANEL_SYNCABLE_TYPES` = the connection declarations whose key is in `FAMILY_PANEL_KEYS`, as `key => item class`. `FAMILY_PANELS` itself stays hand-kept until A3. - `#sync` finds the key instead of indexing the hash with `params[:provider_key]`, the pattern `connect_form` already uses for `FAMILY_PANELS`: once the hash is not a literal, Brakeman flags the indexed lookup as UnsafeReflection (the class name never comes from the param). - The `provider:family` generator no longer patches the hash (`append_panel_syncable_type` removed); a generated adapter already declares its connection. Its two tests now cover `family_panel_items` only; `docs/api/rails_provider_generator.md` updated. No visible change. ## Demo data No demo data needed. ## Migration notes No migration. ## Verification - Old vs new (Rails runner against the removed literal): 26 = 26 entries, equal as a hash. - New tests: every family panel maps to its own `Syncable` `_items` model; `POST sync` for a provider without a panel (Plaid) schedules nothing. The Plaid test is a boundary guard for the new lookup and passes before the change too. - Mutations, each caught: no panel filter (all 28 declarations); a panel provider without a declaration; the wrong declaration field as the type; Brex declaring Mercury's item class. - `eager_load!` then `PANEL_SYNCABLE_TYPES.size` = 26. - `bin/rails test` (full): 15,867 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 - `PANEL_SYNCABLE_TYPES` readers: `#sync` (unknown or panel-less keys still redirect with no notice) and `compute_provider_sync_health` (iterates pairs; order irrelevant). - The generator: one fewer patched list; `FAMILY_PANELS`, `family_panel_items` and `provider_summary` are still patched until their own Track A changes. --- ## Summary by cubic Derives `Settings::ProvidersController::PANEL_SYNCABLE_TYPES` from the adapters' connection declarations instead of a hand-kept hash, so sync support follows a panel's declaration automatically. No visible behavior change. **Refactors** - `PANEL_SYNCABLE_TYPES` is built from connection declarations whose key is in `FAMILY_PANEL_KEYS`, mapping keys to item classes. - `#sync` finds its key with `find` instead of indexing the hash with `params[:provider_key]`, which Brakeman reads as the param choosing the class to constantize. - The `provider:family` generator no longer patches the hash; a generated adapter already declares its connection. - New tests verify every family panel maps to its own syncable item model (via `sync_later`) and that syncing a non-panel provider schedules nothing. Written for commit 4ad3a3bd2ce18f579b4431a51e373b7daa6c2da8. Summary will update on new commits. View guided diff Turn on auto-fix ## Summary by CodeRabbit * **Bug Fixes** * Sync controls now reflect the providers available in the settings panel. Attempting to sync a provider without a panel entry no longer queues a sync or displays a success notice. * **Documentation** * Updated provider setup guidance to clarify how connection declarations determine sync status and the availability of the Sync button. --- .../settings/providers_controller.rb | 39 +++++-------------- docs/api/rails_provider_generator.md | 5 ++- .../provider/family/family_generator.rb | 14 ++----- .../settings/providers_controller_test.rb | 22 +++++++++++ .../provider/family_generator_test.rb | 25 ++++++------ 5 files changed, 49 insertions(+), 56 deletions(-) diff --git a/app/controllers/settings/providers_controller.rb b/app/controllers/settings/providers_controller.rb index c62b79e6a0..30d16bb00a 100644 --- a/app/controllers/settings/providers_controller.rb +++ b/app/controllers/settings/providers_controller.rb @@ -98,7 +98,9 @@ class Settings::ProvidersController < ApplicationController def sync provider_key = params[:provider_key] - syncable_type = PANEL_SYNCABLE_TYPES[provider_key] + # Not PANEL_SYNCABLE_TYPES[provider_key]: Brakeman reads that as the param + # choosing the class to constantize. + _key, syncable_type = PANEL_SYNCABLE_TYPES.find { |key, _type| key == provider_key } return redirect_to settings_providers_path unless syncable_type items = syncable_type.constantize.where(family: Current.family).syncable @@ -237,35 +239,12 @@ class Settings::ProvidersController < ApplicationController FAMILY_PANEL_KEYS = FAMILY_PANELS.map { |p| p[:key] }.freeze FAMILY_PANELS_BY_KEY = FAMILY_PANELS.index_by { |p| p[:key] }.freeze - # Maps panel key → ActiveRecord model name for sync health queries - PANEL_SYNCABLE_TYPES = { - "akahu" => "AkahuItem", - "up" => "UpItem", - "monobank" => "MonobankItem", - "fio" => "FioItem", - "simplefin" => "SimplefinItem", - "lunchflow" => "LunchflowItem", - "redbark" => "RedbarkItem", - "enable_banking" => "EnableBankingItem", - "truelayer" => "TruelayerItem", - "coinstats" => "CoinstatsItem", - "wise" => "WiseItem", - "mercury" => "MercuryItem", - "brex" => "BrexItem", - "coinbase" => "CoinbaseItem", - "binance" => "BinanceItem", - "kraken" => "KrakenItem", - "coinspot" => "CoinspotItem", - "onchain_wallet" => "OnchainWalletItem", - "snaptrade" => "SnaptradeItem", - "questrade" => "QuestradeItem", - "sharesight" => "SharesightItem", - "ibkr" => "IbkrItem", - "trading212" => "Trading212Item", - "trade_republic" => "TradeRepublicItem", - "indexa_capital" => "IndexaCapitalItem", - "sophtron" => "SophtronItem" - }.freeze + # Maps panel key → ActiveRecord model name for sync health queries, from the + # connection each panel's adapter declares. + PANEL_SYNCABLE_TYPES = Provider::Factory.connection_declarations + .select { |declaration| FAMILY_PANEL_KEYS.include?(declaration[:key]) } + .to_h { |declaration| [ declaration[:key], declaration[:type] ] } + .freeze def load_provider_items(provider_key) case provider_key diff --git a/docs/api/rails_provider_generator.md b/docs/api/rails_provider_generator.md index fdfa33f6b4..ac7364e66d 100644 --- a/docs/api/rails_provider_generator.md +++ b/docs/api/rails_provider_generator.md @@ -555,8 +555,9 @@ end - Adds `@my_bank_items` instance variable - Adds a `FAMILY_PANELS` entry, so Bank sync lists My Bank and opens its panel in the drawer. `FAMILY_PANEL_KEYS` also keeps it out of the global provider forms. -- Adds `my_bank` to `PANEL_SYNCABLE_TYPES` and `family_panel_items`, which give its - connection row a sync status and a working Sync button. +- Adds `my_bank` to `family_panel_items`, which with the adapter's `declares_connection` + (from which `PANEL_SYNCABLE_TYPES` is derived) gives its connection row a sync status + and a working Sync button. **File:** `app/helpers/settings_helper.rb` (updated) - Adds a `"my_bank"` case to `provider_summary`, so a connected My Bank is listed under diff --git a/lib/generators/provider/family/family_generator.rb b/lib/generators/provider/family/family_generator.rb index 3f1ad73a50..694576aee1 100644 --- a/lib/generators/provider/family/family_generator.rb +++ b/lib/generators/provider/family/family_generator.rb @@ -370,8 +370,9 @@ class Provider::FamilyGenerator < Rails::Generators::NamedBase # Bank sync renders a provider's connection row and drawer from FAMILY_PANELS, and # FAMILY_PANEL_KEYS keeps it out of the global provider forms. A connected provider # moves under Your connections through its provider_summary case, and the row's sync - # status and Sync button read PANEL_SYNCABLE_TYPES and family_panel_items. The row - # renders the panel partial, so --skip-view skips all four. + # status and Sync button read family_panel_items (PANEL_SYNCABLE_TYPES follows from + # the adapter's declares_connection). The row renders the panel partial, so + # --skip-view skips all three. def add_bank_sync_entries return if options[:skip_view] @@ -379,9 +380,6 @@ class Provider::FamilyGenerator < Rails::Generators::NamedBase add_bank_sync_entry(controller_path, "FAMILY_PANELS", "key: \"#{file_name}\"") do |content| self.class.append_family_panel_entry(content, key: file_name, title: class_name.titleize) end - add_bank_sync_entry(controller_path, "PANEL_SYNCABLE_TYPES", "=> \"#{class_name}Item\"") do |content| - self.class.append_panel_syncable_type(content, key: file_name, class_name: class_name) - end add_bank_sync_entry(controller_path, "family_panel_items", "=> @#{file_name}_items") do |content| self.class.append_family_panel_item(content, key: file_name) end @@ -655,12 +653,6 @@ class Provider::FamilyGenerator < Rails::Generators::NamedBase %({ key: "#{key}", title: "#{title}", turbo_id: "#{key}", partial: "#{key}_panel" })) end - # Maps the panel key to its item model in PANEL_SYNCABLE_TYPES, which the row's sync - # status and Settings::ProvidersController#sync read. - def self.append_panel_syncable_type(content, key:, class_name:) - append_hash_entry(content, /(PANEL_SYNCABLE_TYPES = \{\n)(.*?)(\n[ \t]*\}\.freeze)/m, key, %("#{class_name}Item")) - end - # Hands the loaded items to the row's sync status through family_panel_items. def self.append_family_panel_item(content, key:) append_hash_entry(content, /(def family_panel_items\n[ \t]*\{\n)(.*?)(\n[ \t]*\}\n)/m, key, "@#{key}_items") diff --git a/test/controllers/settings/providers_controller_test.rb b/test/controllers/settings/providers_controller_test.rb index b398bb36b9..0269a2bac2 100644 --- a/test/controllers/settings/providers_controller_test.rb +++ b/test/controllers/settings/providers_controller_test.rb @@ -690,6 +690,28 @@ class Settings::ProvidersControllerTest < ActionDispatch::IntegrationTest assert_match(/Sync started/i, response.body) end + test "every family panel syncs its provider's own connection model" do + types = Settings::ProvidersController::PANEL_SYNCABLE_TYPES + + assert_equal Settings::ProvidersController::FAMILY_PANEL_KEYS.sort, types.keys.sort + types.each do |key, type| + model = type.constantize + assert model.method_defined?(:sync_later), "#{key} syncs a model that cannot sync" + assert_equal "#{key}_items", model.table_name, "#{key} syncs another provider's model" + end + end + + test "POST sync for a provider without a family panel schedules nothing" do + Sync.where(syncable_type: "PlaidItem").delete_all + + assert_no_enqueued_jobs only: SyncJob do + post sync_provider_settings_providers_path(provider_key: "plaid") + end + + assert_redirected_to settings_providers_path + assert_nil flash[:notice] + end + test "GET show includes Interactive Brokers in bank sync providers" do get settings_providers_url diff --git a/test/lib/generators/provider/family_generator_test.rb b/test/lib/generators/provider/family_generator_test.rb index 7fed5dc371..f7dbdda3f6 100644 --- a/test/lib/generators/provider/family_generator_test.rb +++ b/test/lib/generators/provider/family_generator_test.rb @@ -309,33 +309,32 @@ class Provider::FamilyGeneratorTest < ActiveSupport::TestCase assert_nil Provider::FamilyGenerator.append_provider_summary_case("module Foo\nend\n", key: "gocardless") end - # The row's sync status reads both maps, and its Sync button posts a key that - # Settings::ProvidersController#sync looks up in PANEL_SYNCABLE_TYPES. - test "adds the provider to the sync maps of the real providers controller" do + # The row's sync status reads family_panel_items. Its Sync button posts a key that + # Settings::ProvidersController#sync looks up in PANEL_SYNCABLE_TYPES, which is + # derived from the adapter's declaration, so it is not patched here. + test "adds the provider to the sync items of the real providers controller" do source = Rails.root.join("app/controllers/settings/providers_controller.rb").read - result = Provider::FamilyGenerator.append_panel_syncable_type(source, key: "gocardless", class_name: "Gocardless") - result = Provider::FamilyGenerator.append_family_panel_item(result, key: "gocardless") + result = Provider::FamilyGenerator.append_family_panel_item(source, key: "gocardless") assert_parses result - syncable = result[/PANEL_SYNCABLE_TYPES = \{\n(.*?)\n\s*\}\.freeze/m, 1].lines(chomp: true) items = result[/def family_panel_items\n\s*\{\n(.*?)\n\s*\}\n/m, 1].lines(chomp: true) - assert_equal %("gocardless" => "GocardlessItem"), syncable.last.squish assert_equal %("gocardless" => @gocardless_items), items.last.squish # The arrows line up with the entry above, however long the longest key is. - assert_equal syncable[-2].index("=>"), syncable.last.index("=>") assert_equal items[-2].index("=>"), items.last.index("=>") end test "keeps a space before the arrow when the new key is longer than the column" do - result = Provider::FamilyGenerator.append_panel_syncable_type(<<~RUBY, key: "gocardless_bank_data", class_name: "GocardlessBankData") - PANEL_SYNCABLE_TYPES = { - "up" => "UpItem" - }.freeze + result = Provider::FamilyGenerator.append_family_panel_item(<<~RUBY, key: "gocardless_bank_data") + def family_panel_items + { + "up" => @up_items + } + end RUBY assert_parses result - assert_includes result, %( "up" => "UpItem",\n "gocardless_bank_data" => "GocardlessBankDataItem"\n}.freeze) + assert_includes result, %( "up" => @up_items,\n "gocardless_bank_data" => @gocardless_bank_data_items\n }\n) end test "reserved item columns exclude family but include family_id" do