Sha256: 51e22ba6ef8e938ddc89950540b8277cf570cc491f1fd9bb7c1bb84850f4617e

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Decidim
  # 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 EventsManager
    # 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
    # extra - a Hash with extra information to be included in the notification.
    #
    # Returns nothing.
    # rubocop:disable Metrics/ParameterLists
    def self.publish(event:, event_class: Decidim::Events::BaseEvent, resource:, affected_users: [], followers: [], extra: {})
      ActiveSupport::Notifications.publish(
        event,
        event_class: event_class.name,
        resource: resource,
        affected_users: affected_users.uniq.compact,
        followers: followers.uniq.compact,
        extra: extra
      )
    end
    # rubocop:enable Metrics/ParameterLists

    # 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.
    def self.subscribe(event, &block)
      ActiveSupport::Notifications.subscribe(event, &block)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-core-0.21.0 app/services/decidim/events_manager.rb
decidim-core-0.20.1 app/services/decidim/events_manager.rb
decidim-core-0.20.0 app/services/decidim/events_manager.rb
decidim-core-0.19.1 app/services/decidim/events_manager.rb
decidim-core-0.18.1 app/services/decidim/events_manager.rb
decidim-core-0.19.0 app/services/decidim/events_manager.rb
decidim-core-0.17.2 app/services/decidim/events_manager.rb
decidim-core-0.18.0 app/services/decidim/events_manager.rb
decidim-core-0.17.1 app/services/decidim/events_manager.rb
decidim-core-0.16.1 app/services/decidim/events_manager.rb
decidim-core-0.17.0 app/services/decidim/events_manager.rb
decidim-core-0.16.0 app/services/decidim/events_manager.rb