Class: Decidim::NotificationGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/notification_generator.rb

Overview

This class handles system events affecting resources and generates a notification for each recipient by scheduling a new ‘Decidim::NotificationGeneratorForRecipientJob` job for each of them. This way we can easily control which jobs fail and retry them, so that we don’t have duplicated notifications.

Instance Method Summary collapse

Constructor Details

#initialize(event, event_class, resource, recipient_ids, extra) ⇒ NotificationGenerator

Initializes the class.

event - A String with the name of the event. event_class - A class that wraps the event. resource - an instance of a class implementing the ‘Decidim::Resource` concern. extra - a Hash with extra information for the event.



16
17
18
19
20
21
22
# File 'app/services/decidim/notification_generator.rb', line 16

def initialize(event, event_class, resource, recipient_ids, extra)
  @event = event
  @event_class = event_class
  @resource = resource
  @recipient_ids = recipient_ids
  @extra = extra
end

Instance Method Details

#generateObject

Schedules a job for each recipient to create the notification. Returns ‘nil` if the resource is not resource or if it is not present.

Returns nothing.



28
29
30
31
32
33
34
35
# File 'app/services/decidim/notification_generator.rb', line 28

def generate
  return unless resource
  return unless event_class.types.include?(:notification)

  recipient_ids.each do |recipient_id|
    generate_notification_for(recipient_id)
  end
end