• Last updated 17 Jul 22


The Trailblazer project started off with a single gem called trailblazer. Currently, the framework consists of around 40 gems. The main gem trailblazer is an umbrella gem with the sole reason to pull all default dependencies for you. Feel free to pick and choose what you need for your applications.

Are you new to TRB? Then it’s probably best to start on our LEARN page and find out more about this framework!


The following list of gems is an overview of the most important core components in Trailblazer.

Gem Summary Dependencies Source
trailblazer-rails Glue code for conveniently using TRB in a Rails project. Adds Controller#run for invoking operations and Controller#cell to render cells.
trailblazer-activity Callable run-time circuits that control the execution flow of your tasks.
trailblazer-activity-dsl-linear DSL that provides Path, Railway and FastTrack activities.
trailblazer-context Context implementation.
trailblazer-option Callables implementation.
trailblazer-endpoint Implement the entire controller flow, from authentication until rendering, via activities.
trailblazer-operation Thin API around FastTrack exposing the "old" call-API named Operation.
trailblazer-macro Provides Nested(), Model() and other macros for everyday usage.
trailblazer-macro-contract Provides the Contract::Validate() macro and friends. reform, dry-validation
trailblazer-developer Activity visualization, tracing, debugging, PRO Editor communication.
trailblazer-test Minitest assertions and tools for fast TRB testing.
trailblazer-rspec Rspec testing extensions.
trailblazer-workflow Long-running activities with process engine, events and collaboration.
trailblazer-activity-implementation DSL for creating activities from PRO Editor exports.

Along with the core gems goes a rich list of eco-system gems.

Gem Summary Dependencies Source
reform Form objects.
cells Generic view components.
representable Maps representation documents from and to Ruby objects.
disposable Decorators on top of your ORM layer.
roar Parse and render REST API documents using representers.

2.1 Migration

Call API

In versions before 2.1, the automatic merging of the params part and the additional options was confusing many new users and an unnecessary step.

# old style
result = Memo::Create.( params, "current_user" => current_user )

The first argument (params) was merged into the second argument using the key “params”. You now pass one hash to call and hence do the merging yourself.

# new style
result = Memo::Create.( params: params, current_user: current_user )

The new call API is much more consistent and takes away another thing we kept explaining to new users - an indicator for a flawed API.

For a soft deprecation, do this in an initializer:

require "trailblazer/deprecation/call"

You will get a bunch of warnings, so fix all Operation.() calls and remove the require again. This is provied by trailblazer-future gem.

In case your steps expose a signature as follows, you’re safe.

class Memo::Create < Trailblazer::Operation
  step :create_model

  def create_model(options, params:, **)
    # ..

By convention, we renamed option to ctx, but it is completely up to you to adopt this.

Nevertheless, the “old style” signatures won’t work anymore.

class Memo::Create < Trailblazer::Operation
  def create_model(options)
    # ..

  # or

  def create_model(model:, **)
    # ..

Neither a single options nor keyword-arguments-only are gonna fly as the new step signature is more restrictive and always requires you to maintain the ctx (or options, if you will) as the first positional argument, then keyword arguments.

Double splat operator ** at the end will be required to in order to discard unused kw args.

You can introduce this change before actual migration to 2.1.

Steps declared as success or failure are now renamed to pass and fail respectively.

class Memo::Create < Trailblazer::Operation
  success :model
  failure :handle_some_error

Change it as follows.

class Memo::Create < Trailblazer::Operation
  pass :model
  fail :handle_some_error

If you are using Rubocop it will probably start complaining about unreachable code because it just so happens that fail is also a Ruby Kernel’s method. One solution to this could be to add a custom rule to .rubocop.yml like this:

    - '*/**/concepts/**/operation/**/*'
    - '*/**/concepts/**/operation/**/*'

There is also a trick that will allow you to do this rename before actually migrating to 2.1. You can put this in an initializer:

Trailblazer::Operation::Pipetree::DSL.send(:alias_method, :pass, :success)
Trailblazer::Operation::Pipetree::DSL.send(:alias_method, :fail, :failure)

This way you could introduce this change before actual migration to 2.1. Just don’t forget to remove it after updating gems to 2.1.

Now every step that may end up in pass_fast or fail_fast signals need an extra option that indicates fast track usage. Change this operation:

class Memo::Create < Trailblazer::Operation
  step :create

  def create(ctx, **)
    Railway.pass_fast! if ctx[:model].save

Use the :fast_track option to let Trailblazer know about the potential new signal being emitted.

class Memo::Create < Trailblazer::Operation
  step :create, fast_track: true # notice the extra option :fast_track

  def create(ctx, **)
    Railway.pass_fast! if ctx[:model].save


The keys for ctx used to be mixed up, some where "longer.strings", some were :symbols. The new context implementation Context::IndifferentAccess now allows to use both.

ctx[:model]  # => same!

This also works for namespaced keys, which you still might find helpful.

ctx[:"contract.default"]  # => same!

On the core level, we use symbol keys, only (e.g. :"contract.default").

The default implementation of the context object can be set by overriding Context.implementation. For example, if you want the old behavior back.

class Trailblazer::Context
  def self.implementation
    Context # old behavior.

Note that the override might be deprecated in favor of a dependency injection.


The Nested macro allows to, well, nest activities or operations, providing a neat way to encapsulate and reuse logic.

In 2.1, the [Subprocess macro] is the standard way for nesting. The Nested macro should only be used when you use the dynamic version where the nested operation is computed at runtime using :builder.

An exception will warn you about the inappropriate usage.

[Trailblazer] Using the `Nested()` macro with operations and activities is deprecated. Replace `Nested(Create)` with `Subprocess(Create)`.

Both the :input and :output options that used to go with Nested(Present, :input: ...) are now a generic option in Trailblazer. Move them behind the macro parenthesis.

# 2.0
Nested(Present, input: :my_input, output: :my_output)

# 2.1
Nested(Present), input: :my_input, output: :my_output

An exception will stop compilation if you fail to obey.

ArgumentError: unknown keyword: input

Behold of another tiny API change! The :output filter signature has changed for a better.

In Trailblazer 2.0, the following signature was allowed.

# 2.0
  output: ->(ctx, mutable_data:, **) {
    {model: mutable_data[:article]} # you used the {mutable_data} keyword argument.

Mind the arguments being passed to :output. The first positional is the original outer context, the context from the nested operation comes as a mutable_data: keyword argument.

In 2.1, those two have swapped.

# 2.1
  output: ->(inner_ctx, article:, **) {
    # {inner_ctx} is {mutable_data}
    {model: article} # you used the {mutable_data} keyword argument.

The inner context from the nested operation comes first, as a positional argument. Note how you can conveniently use keyword arguments to access variables from this inner ctx (e.g. article:). Keep in mind, the naming is completely up to you. We use inner_ctx/ctx and original_ctx in our code.

If you also need the original context in the :output filter, use the :output_with_outer_ctx option.

# 2.1
  output_with_outer_ctx: true
  output: ->(inner_ctx, original_ctx, article:, **) {
      model:  article,
      errors: original_ctx[:errors].merge(inner_ctx[:errors]) # you can access the outer ctx!

The original, outer ctx comes in as a second positional argument.

You do not need to extend Uber::Callable in classes with #call methods anymore.

Check the Variable Mapping docs for a complete explanation.

Don’t forget to declare fast track usage if you expect it from within nested operation, like this:

Nested(Present), input: :my_input, output: :my_output, fast_track: true

Another difference is that in 2.0, when you were using pass_fast in nested operations, it would stop only the nested operation from executing. After this the outer one would continue executing.

Now returning pass_fast in nested operation will stop both, inner and outer operation with success as a final result. If you rely on old behaviour you can still have it with proper signals mapping:

Nested(Present), input: :my_input,
                 output: :my_output,
                 fast_track: true,
                 Output(:pass_fast) => Track(:success), # pass_fast now mapped to `just` a success
                 Output(:fail_fast) => End(:failure)

Macro API

Macros are functions that add a task along with specific options to the activity. In TRB 2.0, those (historically camel-cased) functions returned an array with two elements.

module MyMacro
  def self.NormalizeParams(name: :myparams, merge_hash: {})
    task = ->((ctx, flow_options), _) do
      ctx[name] = ctx[:params].merge(merge_hash)

      return Trailblazer::Activity::Right, [ctx, flow_options]

    [task, name: name] # old API

In 2.1, a hash is returned. Note that :name is :id now.

module MyMacro
  def self.NormalizeParams(name: :myparams, merge_hash: {})
    task = ->((ctx, flow_options), _) do
      ctx[name] = ctx[:params].merge(merge_hash)

      return Trailblazer::Activity::Right, [ctx, flow_options]

    # new API
      task: task,
      id:   name

This allows for a much richer macro experience where you might add additional steps via a macro, use DSL options such as :before and :after and add taskWrap extensions. [macro API]

A common error if you don’t return a hash from your macro is a call on Array.

   undefined method `call' for #<Array:0x000055663116be98>
 # gems/trailblazer-context-0.4.0/lib/trailblazer/option.rb:9:in `call!'

In Trailblazer 2.0, macros were automatically using the task interface. Mind the arguments and the return value.

task = ->(ctx, current_user:, model:, **) {
  # do something
  # return true/false

In Trailblazer 2.1, macros use the circuit interface per default. If you want the simpler task interface, you need to wrap your task using Binary().

def MyMacro()
  task = ->(ctx, current_user:, model:, **) {
    # do something
    # return true/false

    task: Trailblazer::Activity::TaskBuilder.Binary(task), # wrap the callable.
    id: "my_macro"

A common error if you don’t wrap your macro is an ArgumentError.

       missing keywords: current_user, model

In case you used the same macro twice in one operation, like this for example:

class Create < Trailblazer::Operation
  step Contract::Persist(method: :sync)
  step Contract::Persist(method: :save!)

You will have to specify id explictly for one of them:

class Create < Trailblazer::Operation
  step Contract::Persist(method: :sync)
  step Contract::Persist(method: :save!), id: "some_custom_unique_id"

Contract DSL

It was possible to define contracts on the operation level using a DSL.

class Create < Trailblazer::Operation
  contract do
    property :id

  step Contract::Build()
  step Contract::Validate()

Since the usability doesn’t outweigh the complexity needed to implement such DSL, we decided to remove that functionality for now.

Instead, use an explicit inline class and the :constant option.

class Create < Trailblazer::Operation
  class Form < Reform::Form
    property :id

  step Contract::Build(constant: Form)
  step Contract::Validate()

Trailblazer loader

Usage of a trailblazer-loader is now discouraged as it’s slower than the ones provided by Rails and it’s error prone.

In short, we decided to adopt the Rails naming scheme and change operation names from User::Create to User::Operation::Create, so the file name and class path are in sync.

Read the details here.

We highly recommend changing this with your upgrade as it highly improves the dev experience.


Trailblazer provides a rich set of developer tools to ease debugging and make modelling processes a pleasant experience.

The developer gem contains tools to help you visualize and debug Trailblazer code. Its development features such as tracing or exception handling are advanced tools and will become your best friends very quickly.


We advise you to alias the Developer constant to something handy, such as ::Dev in your project. For instance, in Rails you’d have the following line in a config/initializers/trailblazer.rb initializer.

# config/initializers/trailblazer.rb
require "trailblazer/developer"
Dev = Trailblazer::Developer


Use wtf? if you want to

  • Debug an exception happening somewhere deep in your code.
  • Find out which step changed the track to failure.
  • Focus on specific step(s) to find out what ctx mutations are happening inside them.

This will run your activity with tracing enabled, and in case of an exception, print the trace path to the closest point where the exception was thrown.

Your activity will be called using the TaskWrap invoker, a possible exception will be caught and the closest task found. It then prints the “stack trace”, however, on a much higher level based on tasks, not methods.

Please note that this is a higher-level debugging tool that does not confront you with a 200-lines stack trace the way Ruby does it, but pinpoints the exceptional code and locates the problem on a task level.

This is possible due to you structuring code into higher abstractions, tasks and activities.

You can focus on specific steps and variables to find out what ctx comes in and what goes out. The focus_on option allows us to capture any key(s) from ctx and print any mutations happening within given steps.

You need to pass one or more step names (either default name or given by explicit :id) to capture the mutations.

class Memo::Create < Trailblazer::Activity::Path
  step :validate
  step :create, id: :create_memo
  # ...
end, [ctx, { focus_on: { steps: [:validate, :create_memo] } }])

By default, focusing will capture whole ctx for given steps. But you can also filter on it by passing one or more keys using variables., [ctx, { focus_on: { variables: [:params], steps: :validate } }])

Internally, when focus_on option is passed, we call Object#inspect method to collect before-after data from the ctx for given steps. This data is then used while rendering the trace. In case you want to customize the data collection behaviour with your own logic, you can pass the default_inspector.
    { params: { text: 'Hydrate!', value: nil } },
      focus_on: { steps: :validate, variables: :params },
      default_inspector: ->(value){ value.nil? ? 'UNKNOWN' : value.inspect }

Inspection runs deeply when ctx contains nested structures like hashes or arrays. It’s concept is very simple and handled in Dev::Trace::Inspector module.

The sole purpose to add Dev::Trace::Inspector module is to make custom inspection possible and efficient while tracing. For example, ActiveRecord::Relation#inspect makes additional queries to fetch top 10 records and generate the output everytime.

To avoid this, Inspector will not call inspect method when it finds such objects (deeply nested anywhere).

Instead, it’ll call AR::Relation#to_sql to get plain SQL query which doesn’t make additional queries and is better to understand in tracing output. There is always a possibility to cater such cases and make wtf tracing better. Contributions are welcome :)

The color_map option allows you to customize default coloring scheme being used to render passed or failed steps.


It often is helpful to visualize an activity. One way is the render helper.

puts Trailblazer::Developer.render(Memo::Create)

The rendered output prints each task, its outputs and where they’re connected to.

We’re working on improved visualization for the console, and for the web editor. If you want to help, please ping us on our chat.

Render Linear


The Developer::Client module provides functions to work with activities and workflows created in the PRO editor.


  • IDs are extracted from the label of the element. They get chomped to remove a potential newline at the end and reduce confusion.
  • It is currently not possible to assign semantics to arrows via the label. In the meantime, use the :query option. # TODO


Context aka ctx (or plain old options) is a core argument-specific data structure for Trailblazer. It provides a generic, ordered read/write interface that collects mutable runtime-computed data while providing access to any compile-time information. It is extracted into its own gem and can also be used independently.


ctx can be initialized when an operation is invoked at the run time or by defining dependencies at the compile time. Inside the operation, it gets passed down to every step with it’s argument position depending on step’s interface. It will contain whatever the most recently executed step has changed and hopefully contains what you’re expecting.

If you want to see what ctx modifications are being performed per step or at specific steps, you can debug it using developer’s focus_on API.

ctx’s purpose is to hold the state of your activity which can also be passed down to other nested activities using Subprocess. You can filter what such activities can or can not “see” using variable mapping. After operation’s execution using public call, the Result object wraps the context for convenient access.

In order to provide the generic interface, scoping and debugging capabilities, the “Hash” argument you provide to an operation is initialized as an instance of Trailblazer::Context::Container to build up the final ctx. This allows us to support more features on top of it like indifferent access, aliasing etc

ctx mimics as “Hash” and also allows you to use Strings or Symbols interchangeably as keys; similar to the params hash in Rails.

result = Memo::Create.(params: { text: "Enjoy an IPA" })

result[:params]     # => { text: "Enjoy an IPA" }
result['params']    # => { text: "Enjoy an IPA" }

All keys are stored as Symbols by default in order to allow them to be accessible as keyword arguments. Note that ctx doesn’t provide interchangeable keys for any nested hashes because of the performance reasons.

Most commonly found keys in ctx are 'contract.default', 'contract.default.params', 'policy.default' etc. It sometimes becomes cumbersome to access them from ctx as they can’t be defined as keyword arguments in steps.

To overcome this, it is possible to define a shorter versions of context keys using aliases. By providing aliases mapping in flow_options[:context_options], context will maintain any mutations being made on the origianl keys with the aliases and vice versa.

options = { params: { text: "Enjoy an IPA" } }
flow_options = {
  context_options: {
    aliases: { 'contract.default': :contract, 'policy.default': :policy },
    container_class: Trailblazer::Context::Container::WithAliases,

# Sorry, this feature is only reliable in Ruby > 2.7
if >="3.0.0")
  result = AliasesExample::Memo::Create.(options, flow_options)
else # Ruby 2.6 etc
  result = AliasesExample::Memo::Create.call_with_flow_options(options, flow_options)

result['contract.default']  # => Memo::Contract::Create
result[:contract]           # => Memo::Contract::Create

flow_options are passed to the nested operations via Subprocess and all given aliases will also be applied in them by default.

class Memo::Create < Trailblazer::Operation
  # ...

  pass :sync

  def sync(ctx, contract:, **)
    # ctx['contract.default'] == ctx[:contract]


Trailblazer::Option is one of the core concept behind operation’s step API, reform’s populator API etc. It makes us possible to accept any kind of callable objects at compile time and execute them at runtime.

class Song::Create < Trailblazer::Operation
  step Authorize				# Module callable
  step :model					# Method callable
  step ->(ctx, model:, **) { puts model }	# Proc callable

It is also a replacement over declarative-option and has been extracted out from trailblazer-context by identifying common callable patterns.


Trailblazer::Option() accepts Symbol, lambda and any other type of callable as an argument. It will be wrapped accordingly to make an executable, so you can evaluate it at runtime.

Passing a Symbol will be treated as a method that’s called on the given exec_context.

option = Trailblazer::Option(:object_id)
option.(exec_context: # => 1234567

Same with the objects responding to .call or #call method.

class CallMe
  def, **options)

option = Trailblazer::Option(CallMe)
option.(keyword_arguments: { message: "hello!" }, exec_context: nil) # => "hello!"

Notice the usage of keyword_arguments while calling an Option(). They need to be mentioned explicitly in order for them to be compatible with ruby 2.7+.

And of course, passing lambdas. They get executed within given exec_context when set.

option = Trailblazer::Option(-> { object_id })
option.(exec_context: # => 1234567