fix(rules): offer the rule prompt unless an existing rule already covers the transaction

Merged
SC
2mo ago
+106-6

Summary

After you categorize a transaction, Sure offers "create a rule?", but only when Transaction#eligible_for_category_rule? is true. That check hid the prompt whenever any rule set the chosen category, whatever the rule's conditions were. So once a category has one rule, you're never offered another for it, even for transactions that rule can't match.

Example: an "Amazon Purchase" rule matching AMAZON.CO.UK. Assign an AMZN Mktp UK payment to "Amazon Purchase", and no prompt appears, though no rule covers it. On a real family with rules on 41 of 65 categories, most categorizations never offered a rule. To add one you had to leave for Settings → Rules.

Change

  • The prompt now shows unless an active rule that sets this category already matches this transaction.
  • New Rule#matches_transaction?(transaction) reuses the rule's own matching_resources_scope, so conditions, visibility and effective_date are exactly what the rule engine applies.
  • Inactive rules no longer suppress the prompt, since they don't categorize anything.
  • The existing guards (prompts disabled, 24h snooze, category actually changed) are unchanged.

Tests

  • test/models/transaction/ruleable_test.rb (new): no rule → eligible; a matching active rule → not eligible; a non-matching rule for the same category → eligible; a matching but inactive rule → eligible; no category → not eligible.
  • TransactionCategoriesControllerTest: assigning a category whose rule doesn't cover the transaction renders the prompt, and assigning one whose rule does cover it doesn't.
  • Full non-system suite passes locally.

Related: #3773 (Dismiss snooze) and #3361 (a permanent "Create rule" button). This PR is independent of both.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Category-rule prompts now appear when existing active rules that set the transaction’s category do not match the transaction, and are omitted when an active rule does match.
    • Transactions without a category are not eligible for a category rule.
    • Inactive rules and rules for other resource types no longer prevent a transaction from being considered eligible.
    • Transactions are now checked against rules’ matching conditions when determining category-rule eligibility.