Module: Audiences::Notifications

Defined in:
lib/audiences/notifications.rb

Overview

Handles notification of audience context changes. The notifications handled by this module are related to the membership composition of a context.

For instance, when a user leaves a group, the app will be notified of changes to the members of that context so it can react to that. When the audience configuration of a context changes, a notification will also be published through ‘Audiences::Notifications`.

Class Method Summary collapse

Class Method Details

.publish(*contexts) ⇒ Object

Notifies that a given audience context was changed

Parameters:



32
33
34
35
36
# File 'lib/audiences/notifications.rb', line 32

def publish(*contexts)
  contexts.each do |context|
    subscriptions[context.owner.class]&.call(context)
  end
end

.subscribe(owner_type, job: nil) { ... } ⇒ Object

Subscribes to audience changes to a specific owner type, either with a background job or a callable block

Parameters:

  • owner_type (Class)

    the type of owners handled by the job or block

  • job (Class<ActiveJob::Base>) (defaults to: nil)

    job that will respond to audience changes

Yields:

  • block that will handle the audience change if a job is not given



24
25
26
# File 'lib/audiences/notifications.rb', line 24

def subscribe(owner_type, job: nil, &cbk)
  subscriptions[owner_type] = job&.method(:perform_later) || cbk
end