Skip to content

Commit a320b9f

Browse files
Better docs on schematized objects + cleanup
1 parent ea076ea commit a320b9f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

activesupport/lib/active_support/event_reporter.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,26 @@ def clear
8787
# # }
8888
#
8989
# The +notify+ API can receive either an event name and a payload hash, or an event object.
90-
# If an event object is used, it will be passed through to subscribers as-is, and the name of the
90+
91+
# ==== Event Objects
92+
#
93+
# If an event object is passed to the +notify+ API, it will be passed through to subscribers as-is, and the name of the
9194
# object's class will be used as the event name.
9295
#
96+
# class UserCreatedEvent
97+
# def initialize(id:, name:)
98+
# @id = id
99+
# @name = name
100+
# end
101+
#
102+
# def to_h
103+
# {
104+
# id: @id,
105+
# name: @name
106+
# }
107+
# end
108+
# end
109+
#
93110
# Rails.event.notify(UserCreatedEvent.new(id: 123, name: "John Doe"))
94111
# # Emits event:
95112
# # {
@@ -99,7 +116,8 @@ def clear
99116
# # source_location: { filepath: "path/to/file.rb", lineno: 123, label: "UserService#create" }
100117
# # }
101118
#
102-
# These objects should represent schematized events and be serializable.
119+
# An event is any Ruby object representing a schematized event. While payload hashes allow arbitrary,
120+
# implicitly-structured data, event objects are intended to enforce a particular schema.
103121
#
104122
# ==== Default Encoders
105123
#
@@ -249,7 +267,6 @@ def initialize(*subscribers, raise_on_error: false, tags: nil)
249267
# source_location: Hash (The source location of the event, containing the filepath, lineno, and label)
250268
#
251269
def subscribe(subscriber)
252-
return if @subscribers.include?(subscriber)
253270
unless subscriber.respond_to?(:emit)
254271
raise ArgumentError, "Event subscriber #{subscriber.class.name} must respond to #emit"
255272
end

activesupport/lib/active_support/testing/event_reporter_assertions.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
module ActiveSupport
55
module Testing
6+
# Provides test helpers for asserting on ActiveSupport::EventReporter events.
67
module EventReporterAssertions
7-
module EventCollector # :nodoc:
8+
module EventCollector # :nodoc: all
89
@subscribed = false
910
@mutex = Mutex.new
1011

@@ -40,6 +41,7 @@ def matches?(name, payload, tags)
4041
true
4142
end
4243
end
44+
4345
class << self
4446
def emit(event)
4547
event_recorders&.each do |events|

0 commit comments

Comments
 (0)