Module: Models::EventPublishable
- Extended by:
- ActiveSupport::Concern
- Included in:
- ApplicationRecord
- Defined in:
- app/concerns/models/event_publishable.rb
Instance Method Summary collapse
-
#publish_event(event_class, data:, stream_name: "#{self.class.name}-#{id}") ⇒ Object
Publishes an event to the event store after the current database transaction commits.
Instance Method Details
#publish_event(event_class, data:, stream_name: "#{self.class.name}-#{id}") ⇒ Object
Publishes an event to the event store after the current database
transaction commits. Wrapping in AfterCommitEverywhere guarantees the
event is not dispatched if the surrounding transaction rolls back.
Usage:
publish_event(Events::ArticlePublished, data: { article_id: id })
publish_event(Events::ThingHappened, data: { id: id }, stream_name: "Custom-#id")
15 16 17 18 19 20 21 22 |
# File 'app/concerns/models/event_publishable.rb', line 15 def publish_event(event_class, data:, stream_name: "#{self.class.name}-#{id}") AfterCommitEverywhere.after_commit do Rails.configuration.event_store.publish( event_class.new(data: data), stream_name: stream_name ) end end |