Sha256: 189564ddfaa76eb9b44c4a6dc34ba1f9b5a985850d345cc39dbe22375417c882

Contents?: true

Size: 1.69 KB

Versions: 39

Compression:

Stored size: 1.69 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 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, user_role, extra) # rubocop:disable Metrics/ParameterLists
      @event = event
      @event_class = event_class
      @resource = resource
      @recipient = recipient
      @user_role = user_role
      @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.save!
    end

    private

    def notification
      @notification ||= Notification.new(
        user: recipient,
        event_class: event_class,
        resource: resource,
        event_name: event,
        extra: extra.merge(received_as: user_role)
      )
    end

    attr_reader :event, :event_class, :resource, :recipient, :user_role, :extra
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.9 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.8 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.7 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.5 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.4 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.3 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.2 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.1 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.0 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.0.rc2 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.26.0.rc1 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.2 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.1 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.0 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.0.rc4 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.0.rc3 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.0.rc2 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.25.0.rc1 app/services/decidim/notification_generator_for_recipient.rb
decidim-core-0.24.3 app/services/decidim/notification_generator_for_recipient.rb