Class: Hyrax::Workflow::NotificationService
- Inherits:
-
Object
- Object
- Hyrax::Workflow::NotificationService
- 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
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(entity:, action:, comment:, user:) ⇒ NotificationService
constructor
A new instance of NotificationService.
- #notifier(notification) ⇒ Object
-
#recipients(notification) ⇒ Hash<String, Array>
A hash with keys being the strategy (e.g. “to”, “cc”) and the values are a list of users.
- #send_notification(notification) ⇒ Object
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
#action ⇒ Object (readonly)
Returns the value of attribute action.
31 32 33 |
# File 'app/services/hyrax/workflow/notification_service.rb', line 31 def action @action end |
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
31 32 33 |
# File 'app/services/hyrax/workflow/notification_service.rb', line 31 def comment @comment end |
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
31 32 33 |
# File 'app/services/hyrax/workflow/notification_service.rb', line 31 def entity @entity end |
#user ⇒ Object (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
-
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
#call ⇒ Object
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.
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 |