Skip email notifications for inactive rules
Closes #445
Upstream issue: we-promise/sure#4196
Upstream draft PR: we-promise/sure#4211 (pull/4211), opened 2026-10-14 from this same branch and commit (cffa5576b).
Base: mirror/upstream-main at a3bb5d991 (upstream main). Branch up/rule-inactive-no-email, head cffa5576b.
Problem
"Apply all" (ApplyAllRulesJob, and rake rules:apply_all) passes family.rules to Rule::Runner, which executes every requested rule whatever its active?, so that its transaction changes apply. Rule::ActionExecutor::SendEmailNotification#execute never checked the rule's own active flag, so an email rule the user had switched off recorded its new matches in NotificationDelivery and enqueued RuleEmailNotificationJob. With we-promise/sure#4089, a rule switched off after a timed-out baseline would email its whole history the same way.
Change
SendEmailNotification#execute returns 0 before NotificationDelivery.record_for when rule.active? is false. Nothing else changes: not ApplyAllRulesJob, Rule::Runner or the other executors. An inactive rule's transaction-changing actions still apply under Apply all.
The inactive run records nothing, so matches made while the rule is off stay unrecorded. Without #4089, the rule's next active run emails them, the same as switching it on and running it today. With #4089, switching it on takes a baseline first.
Blast radius
Who else hits this line, and what do they pass? execute is reached only through Rule::Action#apply (app/models/rule/action.rb:55) ← Rule#apply (rule.rb:153) ← Rule::Runner#execute (runner.rb:115). The runner is built by:
| Caller | Rules passed | Effect of the guard |
|---|---|---|
ApplyAllRulesJob (apply_all_rules_job.rb:7), from RulesController#apply_all | family.rules, inactive included | inactive email rules stop emailing (the fix) |
lib/tasks/rules.rake:24 (rules:apply_all) | family.rules, inactive included | same as above |
ApplyRulesJob (apply_rules_job.rb:10), sync and nightly | family.rules.where(active: true) | none |
RuleJob (rule_job.rb:9), from Rule#apply_later ← RulesController#apply (:55-56) and the assistant's apply_rule (apply_rule.rb:84,95) | [rule], after both callers switch it on | none, unless the rule is switched off again before the job runs; then it sends nothing, which is intended |
Rules above the requested ones (runner.rb:59) | active only | none |
Sibling grep:
SendEmailNotificationis constructed only inapp/models/rule/registry/transaction_resource.rb:35.NotificationDelivery.record_foris called atsend_email_notification.rb:20(guarded now) andrule/action.rb:92(seed_notification_baseline). The baseline is left alone deliberately: it records without enqueueing, and it is what makes a re-enabled rule skip its history.RuleEmailNotificationJobis enqueued only by this executor.executor.executehas one caller,Rule::Action#apply.
Tests
test/jobs/apply_all_rules_job_test.rb. Each email rule has its existing match recorded, as its baseline would leave it, and then one new match:
- does not email for an inactive rule: no
RuleEmailNotificationJob, and theNotificationDeliverycount for the rule is unchanged. Fails on unfixed code: 0 jobs expected, 1 enqueued. - applies an inactive rule's category action but does not email: the new match's category goes from nil to Groceries, and no email is sent or recorded. Fails on unfixed code: 1 job enqueued.
- emails new matches for an active rule: one job with
[rule.id, [new_match.id]], and the delivery count goes up by 1. Passes before and after. - does not re-send an active rule's already recorded matches: no job, and the count is unchanged. Passes before and after.
test/models/rule/action_executor/send_email_notification_test.rb:
- an inactive rule records and enqueues nothing: no job, delivery count unchanged, returns 0. Fails on unfixed code: 1 job enqueued.
- The existing tests used
rules(:one), which is inactive by the column default (active default: false), so setup now switches it on. Without that, three existing tests fail on the guard. The watermark test's rule is createdactive: truefor the same reason. Without that, its "sends nothing" assertion would pass because of the guard rather than the baseline.
Mutations, each run against both files:
- Drop the guard: the three inactive tests fail (1 job enqueued).
- Invert it (
return 0 if rule.active?): 7 fail, including the active-rule job test and the three existing executor send tests. - Remove the setup switch-on: the three existing executor send tests fail.
- Disable
seed_notification_baseline: the watermark test fails.
Merge with the open rule PRs (git merge-tree, then trial merge and tests)
- we-promise/sure#4089 (
up/rule-text-operators,c9f239baf): clean. This should land before or with #4089. With the merge, its tests plus the rule, job and assistant tests pass (546 runs, 0 failures), includingnotification_baseline_on_enable_test.rb. - we-promise/sure#4150 (
up/rule-email-backfill,e17833f5c): clean. With the merge, 523 runs, 0 failures. - we-promise/sure#4176 (
up/rule-first-run-baseline,3b4a64d4c) has one textual conflict, insend_email_notification_test.rb's watermark test. To resolve it, take #4176's version and addactive: trueto itsrules.create!. There is also one semantic conflict, in #4176'supdate_rule_test.rbtest "resending an unchanged email action keeps the baseline, so a pending match is still emailed".update_ruleswitches the rule off when its actions are replaced, and the test then callsRule#applyon the inactive rule and expects an email. With the guard, no email is sent. Production goes throughapply_rule, which switches the rule on first. Addingrule.update_columns(active: true)before theapplymakes it pass (10 runs, 0 failures). Whichever of the two PRs lands second carries that line.
Validation
- Full suite on
cffa5576b: 11826 runs, 54575 assertions, 0 failures, 0 errors, 34 skips. bin/rubocop -f githubon the changed files: no offences.bin/brakeman --no-pager -q: 0 warnings.- No ERB, JS, migration or locale changes.