The personal finance app for everyone (by everyone)
Ruby
Add per-account toggle to disable automatic transaction categorization (#2636)
Mike Lloydfc0581fba324
diff --git a/app/controllers/concerns/accountable_resource.rb b/app/controllers/concerns/accountable_resource.rbindex 69f0a2696e..1b3e8c040f 100644--- a/app/controllers/concerns/accountable_resource.rb+++ b/app/controllers/concerns/accountable_resource.rb@@ -112,6 +112,7 @@ module AccountableResource:name, :balance, :subtype, :currency, :accountable_type, :return_to,:opening_balance_date,:institution_name, :institution_domain, :notes, :exclude_from_reports,+ :enable_category_matcher,accountable_attributes: self.class.permitted_accountable_attributes)enddiff --git a/app/models/account/linkable.rb b/app/models/account/linkable.rbindex b91d4ed833..f079331e2c 100644--- a/app/models/account/linkable.rb+++ b/app/models/account/linkable.rb@@ -69,6 +69,13 @@ module Account::Linkableaccount_providers.exists?(provider_type: provider_type)end+ # Whether this account's provider applies the category matcher to imported+ # transactions. Only Plaid honors `enable_category_matcher` today; extend this+ # when other providers (e.g. SimpleFIN) wire up category matching.+ def supports_category_matcher?+ plaid_account.present? || linked_to?("PlaidAccount")+ end+# Check if holdings can be deleted# If account has multiple providers, returns true only if ALL providers allow deletion# This prevents deleting holdings that would be recreated on next syncdiff --git a/app/models/plaid_entry/processor.rb b/app/models/plaid_entry/processor.rbindex c0a8900384..cf2e1ff26a 100644--- a/app/models/plaid_entry/processor.rb+++ b/app/models/plaid_entry/processor.rb@@ -69,6 +69,7 @@ class PlaidEntry::Processordef matched_categoryreturn nil unless detailed_category+ return nil unless account&.enable_category_matcher?@matched_category ||= category_matcher.match(detailed_category)enddiff --git a/app/models/simplefin_account/transactions/processor.rb b/app/models/simplefin_account/transactions/processor.rbindex 12fc46fdd1..87f448a35a 100644--- a/app/models/simplefin_account/transactions/processor.rb+++ b/app/models/simplefin_account/transactions/processor.rb@@ -52,6 +52,11 @@ class SimplefinAccount::Transactions::Processorprivate+ # TODO: When SimpleFIN category matching is wired up (SimplefinAccount::Transactions::CategoryMatcher+ # does not exist yet and this method is currently unused), apply the same+ # `account&.enable_category_matcher?` guard used in PlaidEntry::Processor#matched_category,+ # and include SimpleFIN in Account::Linkable#supports_category_matcher? so the toggle+ # appears for SimpleFIN-linked accounts.def category_matcher@category_matcher ||= SimplefinAccount::Transactions::CategoryMatcher.new(family_categories)enddiff --git a/app/views/accounts/_form.html.erb b/app/views/accounts/_form.html.erbindex c0c80bf751..60be71cdc1 100644--- a/app/views/accounts/_form.html.erb+++ b/app/views/accounts/_form.html.erb@@ -24,6 +24,16 @@<%= yield form %>+ <% if account.persisted? && account.supports_category_matcher? %>+ <div class="flex items-center justify-between gap-3 py-2">+ <div class="space-y-1">+ <p class="text-sm text-primary"><%= t(".enable_category_matcher_label") %></p>+ <p class="text-secondary text-sm"><%= t(".enable_category_matcher_hint") %></p>+ </div>+ <%= form.toggle :enable_category_matcher %>+ </div>+ <% end %>+<details class="group"><summary class="cursor-pointer text-sm text-secondary hover:text-primary flex items-center gap-1 py-2"><%= icon "chevron-right", size: "sm", class: "group-open:rotate-90 transition-transform" %>diff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.ymlindex 398bfa38ab..c5433efe76 100644--- a/config/locales/views/accounts/en.yml+++ b/config/locales/views/accounts/en.yml@@ -51,6 +51,8 @@ en:notes_label: Notesnotes_placeholder: Store additional information like account numbers, sort codes, IBAN, routing numbers, etc.exclude_from_reports: Exclude from all reports+ enable_category_matcher_label: Enable category matcher+ enable_category_matcher_hint: When enabled, the provider's suggested category is applied to imported transactions. Disable to leave new transactions uncategorized.index:accounts: Accountscancel_sync: Cancel syncdiff --git a/db/migrate/20260708000000_add_enable_category_matcher_to_accounts.rb b/db/migrate/20260708000000_add_enable_category_matcher_to_accounts.rbnew file mode 100644index 0000000000..07b0e15e49--- /dev/null+++ b/db/migrate/20260708000000_add_enable_category_matcher_to_accounts.rb@@ -0,0 +1,5 @@+class AddEnableCategoryMatcherToAccounts < ActiveRecord::Migration[7.2]+ def change+ add_column :accounts, :enable_category_matcher, :boolean, default: true, null: false+ end+enddiff --git a/db/schema.rb b/db/schema.rbindex 19a0d2907d..db61280999 100644--- a/db/schema.rb+++ b/db/schema.rb@@ -120,6 +120,7 @@ ActiveRecord::Schema[7.2].define(version: 2026_07_14_120000) dot.datetime "disabled_at"t.boolean "exclude_from_reports", default: false, null: falset.integer "account_providers_count", default: 0, null: false+ t.boolean "enable_category_matcher", default: true, null: falset.index ["accountable_id", "accountable_type"], name: "index_accounts_on_accountable_id_and_accountable_type"t.index ["accountable_type"], name: "index_accounts_on_accountable_type"t.index ["currency"], name: "index_accounts_on_currency"diff --git a/test/controllers/depositories_controller_test.rb b/test/controllers/depositories_controller_test.rbindex 9ce0eb70bf..192823a48d 100644--- a/test/controllers/depositories_controller_test.rb+++ b/test/controllers/depositories_controller_test.rb@@ -38,4 +38,31 @@ class DepositoriesControllerTest < ActionDispatch::IntegrationTestcreated = Account.order(:created_at).lastassert_redirected_to account_path(created) # not the external URLend++ test "update persists enable_category_matcher through the shared update action" do+ linked_account = accounts(:connected)+ assert linked_account.enable_category_matcher?++ patch depository_path(linked_account), params: {+ account: { enable_category_matcher: "0" }+ }++ refute linked_account.reload.enable_category_matcher?++ patch depository_path(linked_account), params: {+ account: { enable_category_matcher: "1" }+ }++ assert linked_account.reload.enable_category_matcher?+ end++ test "edit form renders category matcher toggle only for accounts that support it" do+ get edit_account_url(accounts(:connected))+ assert_response :success+ assert_select "input[type=checkbox][name='account[enable_category_matcher]']", 1++ get edit_account_url(accounts(:depository))+ assert_response :success+ assert_select "input[name='account[enable_category_matcher]']", 0+ endenddiff --git a/test/models/account/linkable_test.rb b/test/models/account/linkable_test.rbindex 560cb56495..ec6ecfb828 100644--- a/test/models/account/linkable_test.rb+++ b/test/models/account/linkable_test.rb@@ -42,6 +42,41 @@ class Account::LinkableTest < ActiveSupport::TestCaserefute @account.linked_to?("SimplefinAccount")end+ test "supports_category_matcher? returns true for Plaid-linked accounts" do+ plaid_account = plaid_accounts(:one)+ AccountProvider.create!(account: @account, provider: plaid_account)++ assert @account.supports_category_matcher?+ end++ test "supports_category_matcher? returns true for legacy plaid_account_id links" do+ plaid_account = plaid_accounts(:one)+ @account.update!(plaid_account: plaid_account)++ assert @account.supports_category_matcher?+ end++ test "supports_category_matcher? returns false for unlinked and non-Plaid accounts" do+ refute @account.supports_category_matcher?++ simplefin_item = SimplefinItem.create!(+ family: @family,+ name: "Test SimpleFin",+ access_url: "https://example.com/access_token"+ )+ simplefin_account = SimplefinAccount.create!(+ simplefin_item: simplefin_item,+ name: "Test Account",+ account_id: "test-acct",+ currency: "USD",+ account_type: "checking",+ current_balance: 0+ )+ @account.update!(simplefin_account: simplefin_account)++ refute @account.supports_category_matcher?+ end+test "can_delete_holdings? returns true for unlinked accounts" doassert @account.unlinked?assert @account.can_delete_holdings?diff --git a/test/models/plaid_entry/processor_test.rb b/test/models/plaid_entry/processor_test.rbindex 730be2e7fc..2078e951cb 100644--- a/test/models/plaid_entry/processor_test.rb+++ b/test/models/plaid_entry/processor_test.rb@@ -89,4 +89,35 @@ class PlaidEntry::ProcessorTest < ActiveSupport::TestCaseassert_equal "Amazon", entry.nameassert_equal categories(:food_and_drink).id, entry.transaction.category_idend++ test "skips category matcher when account.enable_category_matcher is false" do+ @plaid_account.current_account.update!(enable_category_matcher: false)++ plaid_transaction = {+ "transaction_id" => "456",+ "merchant_name" => "Amazon",+ "amount" => 100,+ "date" => Date.current,+ "iso_currency_code" => "USD",+ "personal_finance_category" => {+ "detailed" => "Food"+ },+ "merchant_entity_id" => "456"+ }++ @category_matcher.expects(:match).never++ processor = PlaidEntry::Processor.new(+ plaid_transaction,+ plaid_account: @plaid_account,+ category_matcher: @category_matcher+ )++ assert_difference [ "Entry.count", "Transaction.count" ], 1 do+ processor.process+ end++ entry = Entry.order(created_at: :desc).first+ assert_nil entry.transaction.category_id+ endend