Class: BetterService::Workflows::Base

Inherits:
Object
  • Object
show all
Includes:
Concerns::Workflowable::Callbacks, DSL, Execution, ResultBuilder, RollbackSupport, TransactionSupport
Defined in:
lib/better_service/workflows/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, params: {}) ⇒ Base

Initialize a new workflow

Parameters:

  • user (Object)

    The current user executing the workflow

  • params (Hash) (defaults to: {})

    Parameters for the workflow



64
65
66
67
68
69
70
71
72
# File 'lib/better_service/workflows/base.rb', line 64

def initialize(user, params: {})
  @user = user
  @params = params
  @context = Workflowable::Context.new(user, **params)
  @executed_steps = []
  @branch_decisions = []
  @start_time = nil
  @end_time = nil
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



58
59
60
# File 'lib/better_service/workflows/base.rb', line 58

def context
  @context
end

#paramsObject (readonly)

Returns the value of attribute params.



58
59
60
# File 'lib/better_service/workflows/base.rb', line 58

def params
  @params
end

#userObject (readonly)

Returns the value of attribute user.



58
59
60
# File 'lib/better_service/workflows/base.rb', line 58

def user
  @user
end

Instance Method Details

#callHash

Main entry point - executes the workflow

Runs before_workflow callbacks, executes the workflow (with or without transaction), and runs after_workflow callbacks. Tracks timing and ensures callbacks run even if execution fails.

Returns:

  • (Hash)

    Result hash with success status, context, and metadata



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/better_service/workflows/base.rb', line 81

def call
  @start_time = Time.current
  @context.called!

  # Run before_workflow callbacks
  run_before_workflow_callbacks(@context)

  # If callbacks failed the context, return early
  if @context.failure?
    @end_time = Time.current
    return build_failure_result
  end

  # Execute workflow with or without transaction
  if self.class._use_transaction
    execute_with_transaction
  else
    execute_workflow
  end
ensure
  @end_time ||= Time.current
  # Always run after_workflow callbacks
  run_after_workflow_callbacks(@context) if @context.called?
end