Class: Hyrax::Workflow::ActionTakenService
- Inherits:
-
Object
- Object
- Hyrax::Workflow::ActionTakenService
- Defined in:
- app/services/hyrax/workflow/action_taken_service.rb
Overview
Responsible for performing additional functions when the given criteria is met.
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.handle_action_taken(target:, action:, comment:, user:) ⇒ Object
For the given target and :action - Find the appropriate “function” to call - Then call that function.
Instance Method Summary collapse
-
#call ⇒ Boolean
Calls all the workflow methods for this action.
-
#initialize(target:, action:, comment:, user:) ⇒ ActionTakenService
constructor
A new instance of ActionTakenService.
-
#process_action(service_name) {|status| ... } ⇒ Object
The result of calling the method.
-
#resolve_service(class_name) ⇒ Class, NilClass
Return nil if unable to locate the class.
Constructor Details
#initialize(target:, action:, comment:, user:) ⇒ ActionTakenService
Returns a new instance of ActionTakenService.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 16 def initialize(target:, action:, comment:, user:) @target = case target when Valkyrie::Resource Hyrax::ChangeSet.for(target) else target end @action = action @comment = comment @user = user end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
30 31 32 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 30 def action @action end |
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
30 31 32 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 30 def comment @comment end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
30 31 32 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 30 def target @target end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
30 31 32 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 30 def user @user end |
Class Method Details
.handle_action_taken(target:, action:, comment:, user:) ⇒ Object
For the given target and :action
-
Find the appropriate “function” to call
-
Then call that function. If the function returns a truthy value, then save the target
9 10 11 12 13 14 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 9 def self.handle_action_taken(target:, action:, comment:, user:) new(target: target, action: action, comment: comment, user: user).call end |
Instance Method Details
#call ⇒ Boolean
Calls all the workflow methods for this action. Stops calling methods if any return falsy
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 34 def call return unless action.triggered_methods.any? success = action.triggered_methods.order(:weight).all? do |method| status = process_action(method.service_name) Hyrax.logger.debug("Result of #{method.service_name} is #{status}") status end return save_target if success Hyrax.logger.error "Not all workflow methods were successful, so not saving (#{target.id})" false end |
#process_action(service_name) {|status| ... } ⇒ Object
Returns the result of calling the method.
50 51 52 53 54 55 56 57 58 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 50 def process_action(service_name) service = resolve_service(service_name) return unless service result = service.call(target: target, comment: comment, user: user) yield(result) if block_given? result end |
#resolve_service(class_name) ⇒ Class, NilClass
Return nil if unable to locate the class
62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 62 def resolve_service(class_name) klass = begin class_name.constantize rescue NameError Hyrax.logger.error "Unable to find '#{class_name}', so not running workflow callback" return nil end return klass if klass.respond_to?(:call) Hyrax.logger.error "Expected '#{class_name}' to respond to 'call', but it didn't, so not running workflow callback" nil end |