The personal finance app for everyone (by everyone)
Ruby
Match Plaid and Up categories to default categories in any locale (#4997)
KRAlphacar38eb99c441d4
diff --git a/app/models/plaid_account/transactions/category_matcher.rb b/app/models/plaid_account/transactions/category_matcher.rbindex 263ec04454..0e537db9ab 100644--- a/app/models/plaid_account/transactions/category_matcher.rb+++ b/app/models/plaid_account/transactions/category_matcher.rb@@ -102,7 +102,12 @@ class PlaidAccount::Transactions::CategoryMatcherendend+ # Default categories are created in the family's locale ("Lebensmittel",+ # "Restaurants & Bars"), and the ASCII-only normalisation below would blank or+ # mangle those names. Compare them by their English default name instead, so the+ # English aliases in the taxonomy still match. Custom names pass through as-is.def normalize_user_category_name(name)- name.to_s.downcase.gsub(/[^a-z0-9]/, " ").strip+ english_name = I18n.with_locale(:en) { Category.localized_default_name_for(name) }+ english_name.to_s.downcase.gsub(/[^a-z0-9]/, " ").stripendenddiff --git a/app/models/up_account/transactions/category_matcher.rb b/app/models/up_account/transactions/category_matcher.rbindex b142bbcd25..5c06b572cc 100644--- a/app/models/up_account/transactions/category_matcher.rb+++ b/app/models/up_account/transactions/category_matcher.rb@@ -80,7 +80,12 @@ class UpAccount::Transactions::CategoryMatcherendend+ # Default categories are created in the family's locale ("Lebensmittel",+ # "Restaurants & Bars"), and the ASCII-only normalisation below would blank or+ # mangle those names. Compare them by their English default name instead, so the+ # English aliases in the taxonomy still match. Custom names pass through as-is.def normalize_user_category_name(name)- name.to_s.downcase.gsub(/[^a-z0-9]/, " ").strip+ english_name = I18n.with_locale(:en) { Category.localized_default_name_for(name) }+ english_name.to_s.downcase.gsub(/[^a-z0-9]/, " ").stripendenddiff --git a/test/models/plaid_account/transactions/category_matcher_test.rb b/test/models/plaid_account/transactions/category_matcher_test.rbindex f01a62889b..14d477c095 100644--- a/test/models/plaid_account/transactions/category_matcher_test.rb+++ b/test/models/plaid_account/transactions/category_matcher_test.rb@@ -133,4 +133,41 @@ class PlaidAccount::Transactions::CategoryMatcherTest < ActiveSupport::TestCaseassert_equal @income, @matcher.match("income_wages")assert_equal @income, @matcher.match("income_other_income")end++ # A family created under another locale gets its default categories named in that+ # language. The matcher must still find them, whatever locale the sync runs in.+ test "matches default categories created in another locale" do+ family = families(:empty)+ family.categories.destroy_all+ I18n.with_locale(:de) { family.categories.bootstrap! }+ matcher = PlaidAccount::Transactions::CategoryMatcher.new(family.categories.to_a)++ expectations = {+ "food_and_drink_groceries" => :groceries,+ "food_and_drink_coffee" => :food_and_drink,+ "travel_lodging" => :travel,+ "transportation_gas" => :transportation,+ "loan_payments_credit_card_payment" => :loan_payments,+ "income_wages" => :income+ }++ [ :en, :de ].each do |sync_locale|+ I18n.with_locale(sync_locale) do+ expectations.each do |plaid_category, default_key|+ expected = I18n.t("models.category.defaults.#{default_key}", locale: :de)+ assert_equal expected, matcher.match(plaid_category)&.name,+ "#{plaid_category} should match the German default category while syncing in #{sync_locale}"+ end+ end+ end+ end++ test "leaves custom non-English category names unmatched" do+ family = families(:empty)+ family.categories.destroy_all+ family.categories.create!(name: "Kaffeekasse")+ matcher = PlaidAccount::Transactions::CategoryMatcher.new(family.categories.to_a)++ assert_nil matcher.match("food_and_drink_coffee")+ endenddiff --git a/test/models/up_account/transactions/category_matcher_test.rb b/test/models/up_account/transactions/category_matcher_test.rbindex 0bd1b7fc0e..a28a91c3ba 100644--- a/test/models/up_account/transactions/category_matcher_test.rb+++ b/test/models/up_account/transactions/category_matcher_test.rb@@ -59,4 +59,16 @@ class UpAccount::Transactions::CategoryMatcherTest < ActiveSupport::TestCaseassert_nil matcher.match("groceries"), "no Groceries category exists, so no match"end++ test "maps Up categories to default categories created in another locale" do+ family = families(:empty)+ family.categories.destroy_all+ I18n.with_locale(:de) { family.categories.bootstrap! }+ matcher = UpAccount::Transactions::CategoryMatcher.new(family.categories.to_a)++ { "groceries" => :groceries, "restaurants-and-cafes" => :food_and_drink, "fuel" => :transportation }.each do |slug, default_key|+ expected = I18n.t("models.category.defaults.#{default_key}", locale: :de)+ assert_equal expected, matcher.match(slug)&.name, "expected '#{slug}' to match the German default category"+ end+ endend