Fix push notification not firing on notification creation

- Rails only applies the last callback when `after_create_commit` and
  `after_update_commit` reference the same method name [1]:

> However, if you use the `after_create_commit` and the
`after_update_commit` callback with the same method name, it will only
allow the last callback defined to take effect, as they both internally
alias to `after_commit` which overrides previously defined callbacks
with the same method name.

- Push notifications were never sent when a notification was first
  created — only when the source was updated
- Replaced the two callbacks with a single `after_save_commit`, which
  fires on both create and update, with the
  `source_id_previously_changed?` guard (true for both new records and
  source changes)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

[1] https://guides.rubyonrails.org/active_record_callbacks.html#aliases-for-after-commit
This commit is contained in:
Rosa Gutierrez
2026-02-20 18:22:41 +01:00
committed by Rosa Gutierrez
parent bb6f2963b4
commit 3e5233239b
2 changed files with 29 additions and 2 deletions
@@ -0,0 +1,28 @@
require "test_helper"
class PushNotifiableTest < ActiveSupport::TestCase
test "enqueues push notification job when notification is created" do
assert_enqueued_with(job: PushNotificationJob) do
users(:david).notifications.create!(
source: events(:layout_published),
creator: users(:jason)
)
end
end
test "enqueues push notification job when notification source changes" do
notification = notifications(:logo_mentioned_david)
assert_enqueued_with(job: PushNotificationJob) do
notification.update!(source: events(:logo_published))
end
end
test "does not enqueue push notification job for other updates" do
notification = notifications(:logo_mentioned_david)
assert_no_enqueued_jobs only: PushNotificationJob do
notification.update!(unread_count: 5)
end
end
end