DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

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

Posted on

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. πŸ”₯

Top comments (0)

Git push

Stop by this week's meme thread!