Files
sure/test/models/plaid_entry/processor_test.rb
Mike Lloyd fc0581fba3 Add per-account toggle to disable automatic transaction categorization (#2636)
* Add per-account toggle to disable automatic transaction categorization

Adds an `enable_category_matcher` boolean (default: true) to accounts so
users can opt out of Plaid's automatic category suggestions on a per-account
basis. When disabled, newly synced transactions arrive uncategorized so
rules or manual assignment take precedence.

- New migration adds `enable_category_matcher` column (default true, null: false)
- `PlaidEntry::Processor#matched_category` gates the CategoryMatcher call on the account flag
- Toggle rendered in the account edit modal for linked accounts (saves via main form submit)
- `AccountableResource#account_params` permits the new field
- `AccountsController#toggle_category_matcher` action added for potential API use
- i18n strings added for label and hint text
- Unit test covers the disabled-matcher path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* Only show category matcher toggle for Plaid-linked accounts

Only PlaidEntry::Processor honors enable_category_matcher, but the toggle
rendered for every linked account, silently doing nothing for other
providers. Adds Account::Linkable#supports_category_matcher? (covering both
the legacy plaid_account_id link and AccountProvider rows) and gates the
form toggle on it. The SimpleFIN TODO now also points at this helper so the
toggle appears once SimpleFIN matching lands.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Add controller tests for category matcher toggle persistence and rendering

Covers the two paths flagged in review as untested: the flag persisting
through the shared AccountableResource#update action via account_params
(both disable and re-enable), and the edit form rendering the toggle only
for accounts where supports_category_matcher? is true.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Signed-off-by: Mike Lloyd <49411532+mike-lloyd03@users.noreply.github.com>
Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-07-17 07:22:50 +02:00

124 lines
3.6 KiB
Ruby

require "test_helper"
class PlaidEntry::ProcessorTest < ActiveSupport::TestCase
setup do
@plaid_account = plaid_accounts(:one)
@category_matcher = mock("PlaidAccount::Transactions::CategoryMatcher")
end
test "creates new entry transaction" do
plaid_transaction = {
"transaction_id" => "123",
"merchant_name" => "Amazon", # this is used for merchant and entry name
"amount" => 100,
"date" => Date.current,
"iso_currency_code" => "USD",
"personal_finance_category" => {
"detailed" => "Food"
},
"merchant_entity_id" => "123"
}
@category_matcher.expects(:match).with("Food").returns(categories(:food_and_drink))
processor = PlaidEntry::Processor.new(
plaid_transaction,
plaid_account: @plaid_account,
category_matcher: @category_matcher
)
assert_difference [ "Entry.count", "Transaction.count", "ProviderMerchant.count" ], 1 do
processor.process
end
entry = Entry.order(created_at: :desc).first
assert_equal 100, entry.amount
assert_equal "USD", entry.currency
assert_equal Date.current, entry.date
assert_equal "Amazon", entry.name
assert_equal categories(:food_and_drink).id, entry.transaction.category_id
provider_merchant = ProviderMerchant.order(created_at: :desc).first
assert_equal "Amazon", provider_merchant.name
end
test "updates existing entry transaction" do
existing_plaid_id = "existing_plaid_id"
plaid_transaction = {
"transaction_id" => existing_plaid_id,
"merchant_name" => "Amazon", # this is used for merchant and entry name
"amount" => 200, # Changed amount will be updated
"date" => 1.day.ago.to_date, # Changed date will be updated
"iso_currency_code" => "USD",
"personal_finance_category" => {
"detailed" => "Food"
}
}
@category_matcher.expects(:match).with("Food").returns(categories(:food_and_drink))
# Create an existing entry
@plaid_account.current_account.entries.create!(
external_id: existing_plaid_id,
source: "plaid",
amount: 100,
currency: "USD",
date: Date.current,
name: "Amazon",
entryable: Transaction.new
)
processor = PlaidEntry::Processor.new(
plaid_transaction,
plaid_account: @plaid_account,
category_matcher: @category_matcher
)
assert_no_difference [ "Entry.count", "Transaction.count", "ProviderMerchant.count" ] do
processor.process
end
entry = Entry.order(created_at: :desc).first
assert_equal 200, entry.amount
assert_equal "USD", entry.currency
assert_equal 1.day.ago.to_date, entry.date
assert_equal "Amazon", entry.name
assert_equal categories(:food_and_drink).id, entry.transaction.category_id
end
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
end
end