instance method
notify
Ruby on Rails 8.1.2
Since v8.1.2Signature
notify(name_or_object, payload = nil, caller_depth: 1, **kwargs)
Reports an event to all registered subscribers. An event name and payload can be provided:
Rails.event.notify("user.created", { id: 123 }) # Emits event: # { # name: "user.created", # payload: { id: 123 }, # tags: {}, # context: {}, # timestamp: 1738964843208679035, # source_location: { filepath: "path/to/file.rb", lineno: 123, label: "UserService#create" } # }
Alternatively, an event object can be provided:
Rails.event.notify(UserCreatedEvent.new(id: 123)) # Emits event: # { # name: "UserCreatedEvent", # payload: #<UserCreatedEvent:0x111>, # tags: {}, # context: {}, # timestamp: 1738964843208679035, # source_location: { filepath: "path/to/file.rb", lineno: 123, label: "UserService#create" } # }
Arguments
-
:payload- The event payload when using string/symbol event names. -
:caller_depth- The stack depth to use for source location (default: 1). -
:kwargs- Additional payload data when using string/symbol event names.
Parameters
-
name_or_objectreq -
payloadopt = nil -
caller_depthkey = 1 -
kwargskeyrest
Source
# File activesupport/lib/active_support/event_reporter.rb, line 363
def notify(name_or_object, payload = nil, caller_depth: 1, **kwargs)
name = resolve_name(name_or_object)
payload = resolve_payload(name_or_object, payload, **kwargs)
event = {
name: name,
payload: payload,
tags: TagStack.tags,
context: context_store.context,
timestamp: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond),
}
caller_location = caller_locations(caller_depth, 1)&.first
if caller_location
source_location = {
filepath: caller_location.path,
lineno: caller_location.lineno,
label: caller_location.label,
}
event[:source_location] = source_location
end
@subscribers.each do |subscriber_entry|
subscriber = subscriber_entry[:subscriber]
filter = subscriber_entry[:filter]
next if filter && !filter.call(event)
subscriber.emit(event)
rescue => subscriber_error
if raise_on_error?
raise
else
ActiveSupport.error_reporter.report(subscriber_error, handled: true)
end
end
nil
end
Defined in activesupport/lib/active_support/event_reporter.rb line 363
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveSupport::EventReporter