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