Module: ActiveInteractor::Context::Status

Included in:
Base
Defined in:
lib/active_interactor/context/status.rb

Overview

Context status methods included by all Base

Author:

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#called!(interactor) ⇒ Array<ActiveInteractor::Base>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Track that an Interactor has been called. The #called! method is used by the interactor being invoked with this context. After an interactor is successfully called, the interactor instance is tracked in the context for the purpose of potential future rollback

Parameters:

Returns:

Since:

  • 1.0.0


16
17
18
# File 'lib/active_interactor/context/status.rb', line 16

def called!(interactor)
  _called << interactor
end

#fail!(errors = nil) ⇒ Object

Fail the context instance. Failing a context raises an error that may be rescued by the calling interactor. The context is also flagged as having failed

Examples:

Fail an interactor context

class MyInteractor < ActiveInteractor::Base
  def perform
    context.fail!
  end
end

MyInteractor.perform!
#=> ActiveInteractor::Error::ContextFailure: <#MyInteractor::Context>

Parameters:

  • errors (ActiveModel::Errors|nil) (defaults to: nil)

    errors to add to the context on failure

Raises:

See Also:

Since:

  • 1.0.0


36
37
38
39
40
# File 'lib/active_interactor/context/status.rb', line 36

def fail!(errors = nil)
  merge_errors!(errors) if errors
  @_failed = true
  raise Error::ContextFailure, self
end

#failure?Boolean Also known as: fail?

Note:

The #failure? method is the inverse of the #success? method

Whether the context instance has failed. By default, a new context is successful and only changes when explicitly failed

Examples:

Check if a context has failed

class MyInteractor < ActiveInteractor::Base
  def perform; end
end

result = MyInteractor.perform
#=> <#MyInteractor::Context>

result.failure?
#=> false

Returns:

  • (Boolean)

    false by default or true if failed

Since:

  • 1.0.0


56
57
58
# File 'lib/active_interactor/context/status.rb', line 56

def failure?
  @_failed || false
end

#rollback!Boolean

Roll back an interactor context. Any interactors to which this context has been passed and which have been successfully called are asked to roll themselves back by invoking their Base#rollback instance methods.

Examples:

Rollback an interactor's context

class MyInteractor < ActiveInteractor::Base
  def perform
    context.fail!
  end

  def rollback
    context.user&.destroy
  end
end

user = User.create
#=> <#User>

result = MyInteractor.perform(user: user)
#=> <#MyInteractor::Context user=<#User>>

result.user.destroyed?
#=> true

Returns:

  • (Boolean)

    true if rolled back successfully or false if already rolled back

Since:

  • 1.0.0


86
87
88
89
90
91
# File 'lib/active_interactor/context/status.rb', line 86

def rollback!
  return false if @_rolled_back

  _called.reverse_each(&:rollback)
  @_rolled_back = true
end

#success?Boolean Also known as: successful?

Note:

the #success? method is the inverse of the #failure? method

Whether the context instance is successful. By default, a new context is successful and only changes when explicitly failed

Examples:

Check if a context is successful

class MyInteractor < ActiveInteractor::Base
  def perform; end
end

result = MyInteractor.perform
#=> <#MyInteractor::Context>

result.success?
#=> true

Returns:

  • (Boolean)

    true by default or false if failed

Since:

  • 1.0.0


107
108
109
# File 'lib/active_interactor/context/status.rb', line 107

def success?
  !failure?
end