Class: CurationConcerns::Workflow::NotificationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/curation_concerns/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.



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

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.



25
26
27
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 25

def action
  @action
end

#commentObject (readonly)

Returns the value of attribute comment.



25
26
27
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 25

def comment
  @comment
end

#entityObject (readonly)

Returns the value of attribute entity.



25
26
27
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 25

def entity
  @entity
end

#userObject (readonly)

Returns the value of attribute user.



25
26
27
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 25

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



11
12
13
14
15
16
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 11

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



27
28
29
30
31
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 27

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

#notifier(notification) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 52

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.



44
45
46
47
48
49
50
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 44

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



33
34
35
36
37
38
39
40
# File 'app/services/curation_concerns/workflow/notification_service.rb', line 33

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