Sha256: 4e68ec65a0f9ad591ef5c16485695bc63af2ccbec64d24b07ba37b4dce64fe7d

Contents?: true

Size: 1.64 KB

Versions: 19

Compression:

Stored size: 1.64 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.
    # recipient_ids - an Array of IDs of the users that will receive the event
    # extra - a Hash with extra information to be included in the notification.
    #
    # Returns nothing.
    def self.publish(event:, event_class: Decidim::Events::BaseEvent, resource:, recipient_ids:, extra: {})
      ActiveSupport::Notifications.publish(
        event,
        event_class: event_class.name,
        resource: resource,
        recipient_ids: recipient_ids.compact.uniq,
        extra: extra
      )
    end

    # 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

19 entries across 19 versions & 1 rubygems

Version Path
decidim-core-0.15.2 app/services/decidim/events_manager.rb
decidim-core-0.15.1 app/services/decidim/events_manager.rb
decidim-core-0.15.0 app/services/decidim/events_manager.rb
decidim-core-0.14.4 app/services/decidim/events_manager.rb
decidim-core-0.14.3 app/services/decidim/events_manager.rb
decidim-core-0.14.2 app/services/decidim/events_manager.rb
decidim-core-0.14.1 app/services/decidim/events_manager.rb
decidim-core-0.13.1 app/services/decidim/events_manager.rb
decidim-core-0.12.2 app/services/decidim/events_manager.rb
decidim-core-0.13.0 app/services/decidim/events_manager.rb
decidim-core-0.12.1 app/services/decidim/events_manager.rb
decidim-core-0.13.0.pre1 app/services/decidim/events_manager.rb
decidim-core-0.12.0 app/services/decidim/events_manager.rb
decidim-core-0.11.2 app/services/decidim/events_manager.rb
decidim-core-0.12.0.pre app/services/decidim/events_manager.rb
decidim-core-0.11.1 app/services/decidim/events_manager.rb
decidim-core-0.11.0.pre1 app/services/decidim/events_manager.rb
decidim-core-0.10.1 app/services/decidim/events_manager.rb
decidim-core-0.10.0 app/services/decidim/events_manager.rb