Twitter GitHub Facebook Instagram dirv.me

Daniel Irvine on building software

Rails, the minimalist way

16 July 2014

Today I installed Rails for the first time--sure, I’m a few years late to the party but I prefer to think of it as being fashionably late. Unfortunately, I’m aware of the bloat associated with Rails (my colleagues are always warning me about it) so I want to keep its footprint to the bare minimum, for now, and then possibly I’ll add in more features later.

This post is a simple walkthrough of the steps I’ve taken to start my minimalist Ruby implementation.

My starting point is an existing Ruby app with a “vanilla” Rack web front-end that is ripe for migration (no pun intended). Since it’s all there already, I don’t have a use for ActiveRecord or any of the modeling constructs of Rails. Instead I’ll focus on adding the following:

  • Controllers and routes
  • Session management
  • View templates
  • Both JSON and HTML responses to HTTP requests

Creating a Rails project

Use rails new -h to figure out all the command switches that will help you skip components which are useless to you. In my case I ran:

rails new RailsUI --skip-git --skip-sprockets --skip-spring --skip-test-unit --skip-active-record --skip-bundle

I’ll explain the command switches here:

  • --skip-git because I’m already in a Git repository
  • --skip-sprockets because I’m not using Sprockets
  • --skip-spring because Spring is just overkill right now
  • --skip-test-unit because I’ll be using RSpec, thanks very much
  • --skip-active-record because my application doesn’t connect to a database
  • --skip-bundle because I know there will still be a whole bunch of unnecessary dependencies written to the Gemfile that I’ll just need to remove.

Fixing Gemfile

Open up the generated Gemfile and take a look. What is all this stuff? I don’t know. I don’t like it. I would prefer to add in these things one at a time, and at a time that makes sense. But not now. So I’m going to delete them all.

The one that scares me most of all is the top one: ` gem ‘rails’ `

I know this rails gem is simply a collection of other packages, because I can see them all listed on the rails page on RubyGems (look for the Runtime Dependencies section). I’m going to remove this dependency and add in only the dependencies that I think I’ll need.

I’m not sending email so I don’t need actionmailer. I don’t use activerecord so I don’t need that. I have no idea what sprockets-rails does but it sounds ominous so I’m prepared to foresake it.

My Gemfile now looks like this: ` gem ‘actionpack’ gem ‘actionview’ gem ‘railties’ gem ‘jquery-rails’ gem ‘unicorn’ gem ‘rspec-rails’ gem ‘bundler’ `

Excellent--now I can get to work with rails generate controller...

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram dirv.me