Class: Hyrax::Workflow::ActionTakenService

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, action:, comment:, user:) ⇒ ActionTakenService

Returns a new instance of ActionTakenService.



15
16
17
18
19
20
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 15

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

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



22
23
24
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 22

def action
  @action
end

#commentObject (readonly)

Returns the value of attribute comment.



22
23
24
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 22

def comment
  @comment
end

#targetObject (readonly)

Returns the value of attribute target.



22
23
24
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 22

def target
  @target
end

#userObject (readonly)

Returns the value of attribute user.



22
23
24
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 22

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



8
9
10
11
12
13
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 8

def self.handle_action_taken(target:, action:, comment:, user:)
  new(target: target,
      action: action,
      comment: comment,
      user: user).call
end

Instance Method Details

#callBoolean

Calls all the workflow methods for this action. Stops calling methods if any return falsy

Returns:

  • (Boolean)

    true if all methods returned a truthy result



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 26

def call
  return unless action.triggered_methods.any?
  success = action.triggered_methods.order(:weight).all? do |method|
    status = process_action(method.service_name)
    Rails.logger.debug("Result of #{method.service_name} is #{status}")
    status
  end

  return target.save if success
  Rails.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.

Parameters:

  • service_name (String)

    the fully qualified class name to run the ‘call` method on

Yield Parameters:

  • status

    the result of calling the method

Returns:

  • the result of calling the method



42
43
44
45
46
47
48
49
50
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 42

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

Parameters:

  • class_name (String)

    the fully qualified class name to run

Returns:

  • (Class, NilClass)

    return nil if unable to locate the class



54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/hyrax/workflow/action_taken_service.rb', line 54

def resolve_service(class_name)
  klass = begin
            class_name.constantize
          rescue NameError
            Rails.logger.error "Unable to find '#{class_name}', so not running workflow callback"
            return nil
          end
  return klass if klass.respond_to?(:call)
  Rails.logger.error "Expected '#{class_name}' to respond to 'call', but it didn't, so not running workflow callback"
  nil
end