Class: Decidim::EventsManager

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/app/services/decidim/events_manager.rb

Overview

This class acts as a wrapper of `ActiveSupport::Notifications`, so that if we ever need to change the API we just have to change it in a single point.

Class Method Summary collapse

Class Method Details

.publish(event:, resource:, event_class: Decidim::Events::BaseEvent, affected_users: [], followers: [], extra: {}, force_send: false) ⇒ Object

Publishes a event through the events channel. It requires the name of an event, a class that handles the event and the resource that received the action.

event - a String representing the event that has happened. Ideally, it should

start with `decidim.events` and include the name of the engine that publishes
it.

event_class - The event class must be a class that wraps the event name and

the resource and builds the needed information to publish the event to
the different subscribers in the system.

resource - an instance of a class that received the event. affected_users - a collection of `Decidim::Users` that are affected by the

event and will receive a notification about it.

followers - a collection of `Decidim::Users` that should be notified about

the event, even though it doesn't affect them directly

force_send - boolean indicating if EventPublisherJob should skip the

`notifiable?` check it performs before notifying. Defaults to __false__.

extra - a Hash with extra information to be included in the notification.

Returns nothing. rubocop:disable Metrics/ParameterLists


27
28
29
30
31
32
33
34
35
36
37
# File 'decidim-core/app/services/decidim/events_manager.rb', line 27

def self.publish(event:, resource:, event_class: Decidim::Events::BaseEvent, affected_users: [], followers: [], extra: {}, force_send: false)
  ActiveSupport::Notifications.publish(
    event,
    event_class: event_class.name,
    resource: resource,
    affected_users: affected_users.uniq.compact,
    followers: followers.uniq.compact,
    force_send: force_send,
    extra: extra
  )
end

.subscribe(event, &block) ⇒ Object

Subscribes to the given event, and runs the block every time that event is received.

event - a String or a RegExp to match against event names.

Returns nothing.


46
47
48
# File 'decidim-core/app/services/decidim/events_manager.rb', line 46

def self.subscribe(event, &block)
  ActiveSupport::Notifications.subscribe(event, &block)
end