cult3

Getting started with Trailblazer and Ruby on Rails

May 04, 2016

Table of contents:

  1. Creating a new Ruby on Rails project
  2. Adding Trailblazer
  3. Setting up the folder structure
  4. Conclusion

Last week I gave my review of Trailblazer, a book on the “high-level architecture” framework (Trailblazer) for Ruby web applications.

I find it inevitable that you will need to introduce abstraction into a web application at a certain stage of the application’s life. This means you can either create your own abstractions, or stand on the shoulders of giants and leverage the work and learning of others that came before you.

Personally I’m not a fan of wasting time solving problems that others have already solved. I much prefer using a battle-hardened Open Source implementation, rather than rolling my own only to discover the lessons that have already been learned by my predecessors.

So with that being said, over the next couple of weeks I’m going to be looking at building a web application using Trailblazer and Ruby on Rails. You don’t actually have to use Ruby on Rails with Trailblazer as the two are completely decoupled, but I’m most familiar with Rails and so that’s why I’ve chosen it.

Creating a new Ruby on Rails project

The first thing to do is to create the new application using the Rails new command:

rails new trailblazer_rails_tutorial

If you have been following along with the Ruby and Ruby on Rails tutorials on Culttt you should be very familiar with this process by now!

If this is the first time that you’ve created a Ruby on Rails application, take a look at Getting started with Ruby on Rails.

Adding Trailblazer

Next we need to add the Trailblazer gems. Add the following gems to your Gemfile:

gem 'trailblazer-rails'
gem 'trailblazer-cells'
gem 'reform', '~> 2.1.0'
gem 'cells-slim'

I use Slim as my preferred templating engine. If you don’t want to use Slim you will need to switch out the correct cells gem for your templating engine of choice.

Setting up the folder structure

One of the big differences Trailblazer introduces from the standard Rails set up is the folder structure. In Trailblazer, files are not organised by technology, but instead organised by concept. This groups related classes together within the same directory.

We can create a new concepts directory that sits under the app directory. Each concept is stored under the concepts directory in it’s own directory that organises all of the related code for that concept.

As we build out this application over the coming weeks you will see how this folder structure makes a lot more sense than the traditional method of organising classes by their type.

Conclusion

One of the beautiful things about Trailblazer is it is completely decoupled from Rails. When I’m evaluating whether to use a certain tool, one of my big considerations is “is this going to be a pain in the arse?”.

The fact that Trailblazer just sits inside your Rails project, but doesn’t mess with anything fundamental to the framework is a big plus in my eyes. As you can see from this tutorial, it’s very simple to add Trailblazer to a project.

If anything, we need to accommodate Rails by implementing certain methods that the framework expects. We’ll see examples of this over the coming weeks.

And of course, if you needed to work on a new project that isn’t going to use Rails, you could still use the tools and techniques of Trailblazer without any worries.

Today’s tutorial has been (admittedly) fairly short and easy, but I needed a way to setup this new mini-series. In next week’s tutorial we will jump right into Trailblazer with a first look at Operations.

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.