May 04, 2016
Table of contents:
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.
The first thing to do is to create the new application using the Rails
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.
Next we need to add the Trailblazer gems. Add the following gems to your
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.
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.
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.