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.



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

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.



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

def action
  @action
end

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#entityObject (readonly)

Returns the value of attribute entity.



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

def entity
  @entity
end

#userObject (readonly)

Returns the value of attribute user.



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

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



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

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



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

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

#notifier(notification) ⇒ Object



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

def notifier(notification)
  class_name = notification.name.classify
  klass = begin
            class_name.constantize
          rescue NameError
            Hyrax.logger.error "Unable to find '#{class_name}', so not sending notification"
            return nil
          end
  return klass if klass.respond_to?(:send_notification)
  Hyrax.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.



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

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



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

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