Skip to content

Conversation

@quiethrs
Copy link

@quiethrs quiethrs commented Jul 27, 2025

Add success! method to Context

This PR adds a success! method to Interactor::Context, providing a symmetrical counterpart to the existing fail! method. This allows interactors to complete successfully and exit early when certain conditions are met.

Problem

Currently, interactors can only exit early using context.fail!, which marks the context as failed. There's no built-in way to exit early with a successful result, forcing developers to either:

  • Continue processing even when early completion is desired
  • Use fail! for successful early exits (which is semantically incorrect)

Solution

Add context.success! method that:

  • Completes the interactor successfully
  • Exits early from the current step
  • Optionally updates the context with additional data
  • Raises Interactor::Success exception (handled by the framework)

Basic Usage

def call
  if context.data.empty?
    context.success!(result: "No data to process")
  end
  
  # Process data only if not empty
  context.result = process_data(context.data)
end

With Context Updates

def call
  if context.user.admin?
    context.success!(message: "Admin access granted", bypass_checks: true)
  end
  
  # Regular processing for non-admin users
  perform_regular_checks
end

In Organizers

class ProcessOrder
  include Interactor::Organizer
  
  organize ValidateOrder, ProcessPayment, SendConfirmation
end

# If ValidateOrder calls context.success!, the organizer stops
# and returns the successful context without running remaining steps

GIF Requirement


This feature follows the same patterns as the existing fail! method, ensuring consistency and familiarity for developers already using the Interactor gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant