Created
May 15, 2025 02:01
-
-
Save JamesAndresCM/9ac85a7b3cd4a97c8b1aa1e63912cc90 to your computer and use it in GitHub Desktop.
notification subscriber
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ApplicationRecord | |
after_commit :send_notification | |
def send_notification | |
puts "sendnotification" | |
ActiveSupport::Notifications.instrument("updated.user", { user: self }) | |
end | |
end | |
# app/subscribers/user_event_subscriber.rb | |
class UserEventSubscriber < ActiveSupport::Subscriber | |
def updated(event) | |
user = event.payload[:user] | |
puts "[SUBSCRIBER] Event: #{event.name}" | |
puts "[SUBSCRIBER] Update user #{user.id} - #{user.email}" | |
end | |
# attach_to :user for use attach inside klass, require the class in subscriber initializer | |
# example : require Rails.root.join("app/subscribers/user_event_subscriber") | |
end | |
# attach to class in initializer | |
UserEventSubscriber.attach_to :user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment