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
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-gitbecause I’m already in a Git repository
--skip-sprocketsbecause I’m not using Sprockets
--skip-springbecause Spring is just overkill right now
--skip-test-unitbecause I’ll be using RSpec, thanks very much
--skip-active-recordbecause my application doesn’t connect to a database
--skip-bundlebecause I know there will still be a whole bunch of unnecessary dependencies written to the
Gemfilethat I’ll just need to remove.
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.
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...