Class: Hyrax::Workflow::NotificationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/workflow/notification_service.rb

Overview

Responsible for determining the appropriate notification(s) to deliver based on the given criteria.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity:, action:, comment:, user:) ⇒ NotificationService

Returns a new instance of NotificationService.



24
25
26
27
28
29
# File 'app/services/hyrax/workflow/notification_service.rb', line 24

def initialize(entity:, action:, comment:, user:)
  @entity = entity
  @action = action
  @comment = comment
  @user = user
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



31
32
33
# File 'app/services/hyrax/workflow/notification_service.rb', line 31

def action
  @action
end

#commentObject (readonly)

Returns the value of attribute comment.



31
32
33
# File 'app/services/hyrax/workflow/notification_service.rb', line 31

def comment
  @comment
end

#entityObject (readonly)

Returns the value of attribute entity.



31
32
33
# File 'app/services/hyrax/workflow/notification_service.rb', line 31

def entity
  @entity
end

#userObject (readonly)

Returns the value of attribute user.



31
32
33
# File 'app/services/hyrax/workflow/notification_service.rb', line 31

def user
  @user
end

Class Method Details

.deliver_on_action_taken(entity:, action:, comment:, user:) ⇒ Object

For the given :entity and :action

  • For each associated notification

    • Generate the type of notification

    • Expand the notification roles to users

    • Deliver the notification to the users

Parameters:

  • entity (Sipity::Entity)
    • the workflow entity

  • action (Sipity::WorkflowAction)
    • the action taken on the entity

  • comment (#comment)
    • the comment associated with the action being taken

  • user (User)
    • the person taking the action



17
18
19
20
21
22
# File 'app/services/hyrax/workflow/notification_service.rb', line 17

def self.deliver_on_action_taken(entity:, action:, comment:, user:)
  new(entity: entity,
      action: action,
      comment: comment,
      user: user).call
end

Instance Method Details

#callObject



33
34
35
36
37
# File 'app/services/hyrax/workflow/notification_service.rb', line 33

def call
  action.notifiable_contexts.each do |ctx|
    send_notification(ctx.notification)
  end
end

#notifier(notification) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/hyrax/workflow/notification_service.rb', line 57

def notifier(notification)
  class_name = notification.name.classify
  klass = begin
            class_name.constantize
          rescue NameError
            Rails.logger.error "Unable to find '#{class_name}', so not sending notification"
            return nil
          end
  return klass if klass.respond_to?(:send_notification)
  Rails.logger.error "Expected '#{class_name}' to respond to 'send_notification', but it didn't, so not sending notification"
  nil
end

#recipients(notification) ⇒ Hash<String, Array>

Returns a hash with keys being the strategy (e.g. “to”, “cc”) and the values are a list of users.

Returns:

  • (Hash<String, Array>)

    a hash with keys being the strategy (e.g. “to”, “cc”) and the values are a list of users.



50
51
52
53
54
55
# File 'app/services/hyrax/workflow/notification_service.rb', line 50

def recipients(notification)
  notification.recipients.each_with_object({}) do |r, h|
    h[r.recipient_strategy] ||= []
    h[r.recipient_strategy] += PermissionQuery.scope_users_for_entity_and_roles(entity: entity, roles: r.role)
  end
end

#send_notification(notification) ⇒ Object



39
40
41
42
43
44
45
46
# File 'app/services/hyrax/workflow/notification_service.rb', line 39

def send_notification(notification)
  notifier = notifier(notification)
  return unless notifier
  notifier.send_notification(entity: entity,
                             comment: comment,
                             user: user,
                             recipients: recipients(notification))
end