The personal finance app for everyone (by everyone)
Ruby
fix(rules): offer the rule prompt unless an existing rule already covers the transaction (#3796)
scragnog2e8483a578ca
diff --git a/app/models/rule.rb b/app/models/rule.rbindex c45b7ba5d0..721a150e3d 100644--- a/app/models/rule.rb+++ b/app/models/rule.rb@@ -48,6 +48,11 @@ class Rule < ApplicationRecordmatching_resources_scope.pluck(:id)end+ # Whether this rule's conditions currently match the given transaction.+ def matches_transaction?(transaction)+ matching_resources_scope.where(id: transaction.id).exists?+ end+# Creates a categorization rule for the Quick Categorize Wizard.# Returns the saved rule, or nil if a duplicate or invalid rule already exists.def self.create_from_grouping(family, grouping_key, category, transaction_type: nil)diff --git a/app/models/transaction/ruleable.rb b/app/models/transaction/ruleable.rbindex aacd1b9239..44f7cbecb2 100644--- a/app/models/transaction/ruleable.rb+++ b/app/models/transaction/ruleable.rb@@ -1,13 +1,19 @@module Transaction::Ruleableextend ActiveSupport::Concern+ # Offer the "create a rule?" prompt unless an active rule that sets this+ # category would already categorize this transaction. Checking only whether+ # *some* rule targets the category hid the prompt for every category that+ # already had a rule, even when that rule's conditions don't match this+ # transaction (e.g. an "AMAZON" rule and an "AMZN Mktp" payee).def eligible_for_category_rule?- rules.joins(:actions).where(- actions: {- action_type: "set_transaction_category",- value: category_id- }- ).empty?+ return false if category_id.blank?++ rules.where(active: true, resource_type: "transaction")+ .joins(:actions)+ .where(actions: { action_type: "set_transaction_category", value: category_id })+ .distinct+ .none? { |rule| rule.matches_transaction?(self) }endprivatediff --git a/test/controllers/transaction_categories_controller_test.rb b/test/controllers/transaction_categories_controller_test.rbindex 116907edc2..d53a2e6bba 100644--- a/test/controllers/transaction_categories_controller_test.rb+++ b/test/controllers/transaction_categories_controller_test.rb@@ -30,4 +30,37 @@ class TransactionCategoriesControllerTest < ActionDispatch::IntegrationTestassert_nil @transaction.reload.category_idassert_nil category.reload.last_used_atend++ test "offers a rule prompt when the chosen category's existing rule does not cover this transaction" do+ category = categories(:income)+ @entry.update!(name: "AMZN Mktp UK")+ rule = @entry.account.family.rules.build(name: "Amazon", resource_type: "transaction", active: true)+ rule.conditions.build(condition_type: "transaction_name", operator: "like", value: "AMAZON.CO.UK")+ rule.actions.build(action_type: "set_transaction_category", value: category.id.to_s)+ rule.save!++ patch transaction_category_url(@entry),+ params: { entry: { entryable_type: "Transaction", entryable_attributes: { id: @transaction.id, category_id: category.id } } },+ as: :turbo_stream++ assert_response :success+ # The prompt is rendered into the turbo-stream response (flash is cleared once rendered).+ assert_match %r{/rules/new\?[^"]*action_value=#{category.id}}, response.body+ end++ test "does not offer a rule prompt when an existing rule already covers this transaction" do+ category = categories(:income)+ @entry.update!(name: "AMZN Mktp UK")+ rule = @entry.account.family.rules.build(name: "Amazon", resource_type: "transaction", active: true)+ rule.conditions.build(condition_type: "transaction_name", operator: "like", value: "AMZN")+ rule.actions.build(action_type: "set_transaction_category", value: category.id.to_s)+ rule.save!++ patch transaction_category_url(@entry),+ params: { entry: { entryable_type: "Transaction", entryable_attributes: { id: @transaction.id, category_id: category.id } } },+ as: :turbo_stream++ assert_response :success+ assert_not_includes response.body, "action_type=set_transaction_category"+ endenddiff --git a/test/models/transaction/ruleable_test.rb b/test/models/transaction/ruleable_test.rbnew file mode 100644index 0000000000..87efce2539--- /dev/null+++ b/test/models/transaction/ruleable_test.rb@@ -0,0 +1,56 @@+require "test_helper"++class Transaction::RuleableTest < ActiveSupport::TestCase+ setup do+ @family = families(:dylan_family)+ @family.rules.destroy_all+ @transaction = transactions(:one)+ @transaction.entry.update!(name: "AMZN Mktp UK")+ @category = categories(:food_and_drink)+ @transaction.update!(category: @category)+ end++ test "eligible when no rule sets the category" do+ assert @transaction.eligible_for_category_rule?+ end++ test "not eligible when an active rule for the category already matches this transaction" do+ create_category_rule(name_like: "AMZN")++ assert_not @transaction.eligible_for_category_rule?+ end++ test "eligible when the category's only rule does not match this transaction" do+ create_category_rule(name_like: "AMAZON.CO.UK")++ assert @transaction.eligible_for_category_rule?+ end++ test "eligible when the matching rule for the category is inactive" do+ create_category_rule(name_like: "AMZN", active: false)++ assert @transaction.eligible_for_category_rule?+ end++ test "ignores rules for other resource types instead of raising" do+ rule = create_category_rule(name_like: "AMZN")+ rule.update_column(:resource_type, "account")++ assert_nothing_raised { assert @transaction.eligible_for_category_rule? }+ end++ test "not eligible without a category" do+ @transaction.update!(category: nil)++ assert_not @transaction.eligible_for_category_rule?+ end++ private+ def create_category_rule(name_like:, active: true)+ rule = @family.rules.build(name: "Test rule", resource_type: "transaction", active: active)+ rule.conditions.build(condition_type: "transaction_name", operator: "like", value: name_like)+ rule.actions.build(action_type: "set_transaction_category", value: @category.id.to_s)+ rule.save!+ rule+ end+end