DEV Community

Cover image for We improved the trailblazer-rails gem!
Nick Sutterer for Trailblazer

Posted on

2 2

We improved the trailblazer-rails gem!

While working on an example Rails application for the new Trailblazer tutorial series I figured out that trailblazer-rails is quite inconvenient to use when you need to customize calls.

Controller-wide cells options

When using the Cells gem for rendering views along with a layout cell, you formerly had to pass the :layout option manually in every controller action.

class SongsController < ApplicationController
  def new
    # ...
    render cell(Song::Cell::New, ctx, layout: App::Cell::Layout)
  end
Enter fullscreen mode Exit fullscreen mode

People started monkey-patching internal methods from trailblazer-rails to get the layout (and other options) automatically injected into every #cell call.

This is built-in now, just define #options_for_cell on the controller.

class SongsController < ApplicationController
  private def options_for_cell(model, options)
    {
      layout: App::Cell::Layout
    }
  end
Enter fullscreen mode Exit fullscreen mode

The hash returned from this method is passed as a defaults hash to #cell. It's documented, too!

Runtime variables for operations

Another clumsiness I noticed was that it's impossible to inject variables into the operation context when using #run. You can simply provide those as keyword arguments now.

class SongsController < ApplicationController
  def create
    run Song::Operation::Create, 
      current_user: current_user, cookie: session do |ctx|
      # ...
    end
Enter fullscreen mode Exit fullscreen mode

This will add :current_user and :cookie to the ctx passed into the Song::Operation::Create operation. Here are the docs.

Trailblazer-Rails docs moved!

In the process of the refactoring we also rewrote trailblazer-rails docs. The old docs are still available on the old website. Why would you need them, though, with new docs waiting here?

The new gem version is 2.2.0. 🔥

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay