Skip to content

Instantly share code, notes, and snippets.

@JamesAndresCM
Created May 15, 2025 02:01
Show Gist options
  • Save JamesAndresCM/9ac85a7b3cd4a97c8b1aa1e63912cc90 to your computer and use it in GitHub Desktop.
Save JamesAndresCM/9ac85a7b3cd4a97c8b1aa1e63912cc90 to your computer and use it in GitHub Desktop.
notification subscriber
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