The personal finance app for everyone (by everyone)
Ruby
Fix duplicate bill suggestions after renaming a bill (#4222)
joaocbatista9cddb584c15a
diff --git a/app/controllers/recurring_occurrences_controller.rb b/app/controllers/recurring_occurrences_controller.rbindex b56f13c65b..279be12829 100644--- a/app/controllers/recurring_occurrences_controller.rb+++ b/app/controllers/recurring_occurrences_controller.rb@@ -166,7 +166,7 @@ class RecurringOccurrencesController < ApplicationController# The names the matcher itself would recognize: the series' own name plus# any alias a manual attach has taught it.def matcher_name_patterns(series)- ([ series.name ] + Array(series.matcher_hints["name_aliases"]))+ ([ series.name, series.matcher_name ] + Array(series.matcher_hints["name_aliases"])).compact_blank.map { |name| "%#{ActiveRecord::Base.sanitize_sql_like(name)}%" }enddiff --git a/app/models/recurring_transaction.rb b/app/models/recurring_transaction.rbindex 047ed38ad5..95c4f4b2a3 100644--- a/app/models/recurring_transaction.rb+++ b/app/models/recurring_transaction.rb@@ -75,6 +75,7 @@ class RecurringTransaction < ApplicationRecordafter_commit :generate_occurrences, on: :createafter_commit :regenerate_future_occurrences, on: :update, if: :schedule_shape_changed?+ before_save :preserve_detected_name, if: -> { name_changed? && name_was.present? && matcher_hints.to_h["detected_name"].blank? }after_commit :pin_amount_on_dates_already_due, on: :update, if: :saved_change_to_amount?# Form state for the frequency picker and the create-bill dialog;@@ -139,6 +140,17 @@ class RecurringTransaction < ApplicationRecordname.presence || merchant&.nameend+ # The name the detector matched on, kept apart from the user-facing name+ # so renaming a bill does not orphan it from its bank transactions.+ def matcher_name+ matcher_hints.to_h["detected_name"].presence || name+ end++ # A bill first renamed without a stored detected name keeps its original name, so the detector can still claim it.+ def preserve_detected_name+ self.matcher_hints = matcher_hints.to_h.merge("detected_name" => name_was)+ end+def merchant_or_name_presentif merchant_id.blank? && name.blank?errors.add(:base, :merchant_or_name_required)@@ -540,7 +552,8 @@ class RecurringTransaction < ApplicationRecordmanual: true,expected_amount_min: expected_min,expected_amount_max: expected_max,- expected_amount_avg: expected_avg+ expected_amount_avg: expected_avg,+ matcher_hints: transaction.merchant_id.present? ? {} : { "detected_name" => entry.name }}create!(attributes)@@ -626,7 +639,7 @@ class RecurringTransaction < ApplicationRecordendelse# Match by entry name- entries.where(name: name)+ entries.where(name: matcher_name)endschedule_matched(candidates)diff --git a/app/models/recurring_transaction/allocator.rb b/app/models/recurring_transaction/allocator.rbindex 662c90d456..38e6e23312 100644--- a/app/models/recurring_transaction/allocator.rb+++ b/app/models/recurring_transaction/allocator.rb@@ -309,7 +309,7 @@ class RecurringTransactionhints = series.matcher_hints.deep_dupif series.merchant_id.blank? && series.name.present?- known = ([ series.name ] + Array(hints["name_aliases"]))+ known = ([ series.matcher_name ] + Array(hints["name_aliases"])).map { |name| Matcher.normalize_name(name) }unless known.include?(Matcher.normalize_name(entry.name))diff --git a/app/models/recurring_transaction/identifier.rb b/app/models/recurring_transaction/identifier.rbindex cca171c08f..4b54dc0d93 100644--- a/app/models/recurring_transaction/identifier.rb+++ b/app/models/recurring_transaction/identifier.rb@@ -197,7 +197,7 @@ class RecurringTransaction# series can legitimately share this key (subscription tiers), which is# exactly why claiming goes through amount-nearness rather than lookup.def identity_key(recurring)- identifier = recurring.merchant_id.present? ? [ :merchant, recurring.merchant_id ] : [ :name, recurring.name ]+ identifier = recurring.merchant_id.present? ? [ :merchant, recurring.merchant_id ] : [ :name, recurring.matcher_name ][ identifier, recurring.currency, recurring.account_id, recurring.destination_account_id ]end@@ -321,7 +321,7 @@ class RecurringTransactionif identifier_type == :merchantrecurring.merchant_id == identifier_valueelse- recurring.merchant_id.nil? && recurring.name == identifier_value+ recurring.merchant_id.nil? && recurring.matcher_name == identifier_valueendendend@@ -423,7 +423,8 @@ class RecurringTransactionbill_type: income ? "income" : classification.bill_type,category_id: income ? nil : classification.category_id,autopay: income ? false : classification.autopay,- manual: false+ manual: false,+ matcher_hints: pattern[:name] ? { "detected_name" => pattern[:name] } : {}))end@@ -485,7 +486,7 @@ class RecurringTransactionif recurring.merchant_id.present?entry.read_attribute("transaction_merchant_id") == recurring.merchant_idelse- entry.name == recurring.name+ entry.name == recurring.matcher_nameendenddiff --git a/app/models/recurring_transaction/matcher.rb b/app/models/recurring_transaction/matcher.rbindex cbdb57d1d7..3c50453f76 100644--- a/app/models/recurring_transaction/matcher.rb+++ b/app/models/recurring_transaction/matcher.rb@@ -375,7 +375,7 @@ class RecurringTransactiondef known_names(series)@known_names ||= {}- @known_names[series.id] ||= ([ series.name ] + Array(series.matcher_hints["name_aliases"]))+ @known_names[series.id] ||= ([ series.name, series.matcher_name ] + Array(series.matcher_hints["name_aliases"])).compact.map { |name| self.class.normalize_name(name) }.to_setenddiff --git a/test/models/recurring_transaction/allocator_test.rb b/test/models/recurring_transaction/allocator_test.rbindex 551a496d14..0f6e72ee09 100644--- a/test/models/recurring_transaction/allocator_test.rb+++ b/test/models/recurring_transaction/allocator_test.rb@@ -36,6 +36,21 @@ class RecurringTransaction::AllocatorTest < ActiveSupport::TestCaseassert_no_match(/translation missing/i, message)end+ test "manual attach does not record the detected name as a new alias after a rename" do+ @rent.update!(name: "Watson Rent", matcher_hints: { "detected_name" => "WATSON PROPERTY MGMT" })+ entry = @account.entries.create!(+ date: Date.current,+ amount: 2000,+ currency: "USD",+ name: "WATSON PROPERTY MGMT",+ entryable: Transaction.new+ )++ @allocator.allocate!(entry: entry)++ assert_not_includes Array(@rent.reload.matcher_hints["name_aliases"]), "WATSON PROPERTY MGMT"+ end+test "a price rise does not re-target an occurrence that has already been paid against" do@occurrence.update!(expected_amount: nil) # inheriting from the series@allocator.allocate!(entry: entry_for(500))diff --git a/test/models/recurring_transaction_test.rb b/test/models/recurring_transaction_test.rbindex 113d979e0b..4105f7ac65 100644--- a/test/models/recurring_transaction_test.rb+++ b/test/models/recurring_transaction_test.rb@@ -1513,6 +1513,52 @@ class RecurringTransactionTest < ActiveSupport::TestCaseassert_equal 0, series.cycles_overdue, "one day late is not one cycle missed"end+ test "identify_patterns_for does not re-suggest a renamed name-matched bill" do+ [ 0, 1, 2 ].each do |months_ago|+ @account.entries.create!(+ date: months_ago.months.ago.beginning_of_month + 5.days,+ amount: 9.99,+ currency: "USD",+ name: "STREAMCO *CHG",+ entryable: Transaction.create!(category: categories(:food_and_drink))+ )+ end++ assert_difference "@family.recurring_transactions.count", 1 do+ RecurringTransaction.identify_patterns_for!(@family)+ end++ bill = @family.recurring_transactions.last+ assert_equal "STREAMCO *CHG", bill.matcher_name++ bill.update!(name: "Streaming")++ assert_no_difference "@family.recurring_transactions.count" do+ RecurringTransaction.identify_patterns_for!(@family)+ end+ assert_equal "STREAMCO *CHG", bill.reload.matcher_name+ end++ test "renaming a legacy bill without a stored detected name keeps its original name for matching" do+ bill = @family.recurring_transactions.create!(+ account: @account,+ name: "LEGACY CO",+ amount: 5,+ currency: "USD",+ expected_day_of_month: 5,+ last_occurrence_date: Date.current,+ next_expected_date: 1.month.from_now.to_date,+ status: "active",+ occurrence_count: 3,+ bill_type: "bill",+ manual: false+ )++ bill.update!(name: "Renamed Legacy")++ assert_equal "LEGACY CO", bill.reload.matcher_name+ end+privatedef stale_hint_series