Sha256: b51d96b17232c0d308bf55cf6017056ef3fafecb8e521b6fa05ba193899420f3

Contents?: true

Size: 1.61 KB

Versions: 28

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Decidim
  # This class generates a notification based on the given event, for the given
  # resource/recipient couple. It is intended to be used by the
  # `Decidim::NotificationGenerator` class, which schedules a job for each recipient
  # of the event, so that we can easily control which jobs fail.
  class NotificationGeneratorForRecipient
    # Initializes the class.
    #
    # event - A String with the name of the event.
    # event_class - The class that wraps the event, in order to decorate it.
    # resource - an instance of a class implementing the `Decidim::Resource` concern.
    # recipient - the ID of the User that will receive the notification.
    # extra - a Hash with extra information to be included in the notification.
    def initialize(event, event_class, resource, recipient_id, extra)
      @event = event
      @event_class = event_class
      @resource = resource
      @recipient_id = recipient_id
      @extra = extra
    end

    # Generates the notification. Returns `nil` if the resource is not resource
    # or if the resource or the user are not present.
    #
    # Returns a Decidim::Notification.
    def generate
      return unless event_class
      return unless resource
      return unless recipient

      Notification.create!(
        user: recipient,
        event_class: event_class,
        resource: resource,
        event_name: event,
        extra: extra
      )
    end

    private

    attr_reader :event, :event_class, :resource, :recipient_id, :extra

    def recipient
      @recipient ||= User.where(id: recipient_id).first
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
decidim-core-0.8.4 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.8.3 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.8.2 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.8.1 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.8.0 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.7.4 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.7.3 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.7.2 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.7.1 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.7.0 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.6.8 app/services/decidim/notification_generator_for_recipient.rb
decidim-0.6.8 decidim-core/app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.6.7 app/services/decidim/notification_generator_for_recipient.rb
decidim-0.6.7 decidim-core/app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.6.6 app/services/decidim/notification_generator_for_recipient.rb
decidim-0.6.6 decidim-core/app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.6.5 app/services/decidim/notification_generator_for_recipient.rb
decidim-0.6.5 decidim-core/app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.6.4 app/services/decidim/notification_generator_for_recipient.rb
decidim-0.6.4 decidim-core/app/services/decidim/notification_generator_for_recipient.rb