Pimp Your Rails Gemfile Development Group

The Rails development-oriented RubyGems scene is out of control right now. The number of high-quality open-source tools you can stuff into your Gemfile and bundle up these days is almost scary. When I start a fresh Rails project, I’m currently injecting a development group that looks something like this:

group :development do
  gem "pry-rails"
  gem "pry-plus"
  gem "awesome_print"
  gem "rack-livereload"
  gem "guard-livereload"
  gem "guard-pow"
  gem "powder"
  gem "terminal-notifier-guard"
  gem "guard-shell"
  gem "quiet_assets"
  gem "spring"
  gem "xray-rails"
  gem "better_errors"
  gem "binding_of_caller"
  gem "sextant"
  gem "letter_opener"
  gem "bullet"

Some of these gems help keep me out of the guts of my code, while some of them thrust me right into the thick of my code’s guts, right where I need to be for a better look. Others make my time and effort more efficient by removing distraction, printing things more cleanly, keeping me from repeating commands, or delivering updates straight to the browser. Here’s a brief run-through:

pry-rails sets the Rails console from plain ol’ irb to the superior pry. pry-plus adds some extra goodies. The list of enhancements that pry brings is pretty extensive but some of my favorites are:

  • You can use ls to list all methods on any Ruby object, analogous to shell command of the same name for listing a folder’s contents.
  • ? <method-name> will print the documentation on the named method.
  • The variable _ will always refer to the output of the last command.
  • wtf? will reveal the file and line number of the last error.

If you want to learn more about pry, Conrad Irwin’s Rails Conf talk is very worth a gander.

awesome_print does exactly its name. It will print your irb or pry command output awesomely with color and alignment. I also enable it globally by adding the following to my ~/.pryrc file:

require "awesome_print"

guard-livereload and rack-livereload will bring any modifications to view-related files right up into the browser for you. Script and template file updates will cause a full page refresh, but updated stylesheets will magically and almost instantly take effect on the screen.

I use pow to manage my multiple development server environments and guard-pow will auto restart a booted server intelligently when pre-loaded files are updated. powder makes running pow commands dead simple.

terminal-notifier-guard brings your guarded action’s messages up through OSX 10.8 User Notifications

guard-shell is for running shell scripts on file changes. My favorite use so far is for auto-optimizing images in the assets folder with ImageOptim:

guard 'shell' do
  watch %r{^app/assets/images/.} do |file|
    n file[0], "#{file[0]} changed"
    `open #{file[0]} -a ImageOptim`

quiet_assets turns off all the noisy and usually irrelevant asset pipeline log details from development.log.

spring is a Rails application preloader, which is a substantial time-saver when hoping in and out of rails console or running rspec. To use it, just prefix your desired command with spring. The first time you run a spring command in a shell, it will keep an instance of the app running in a background process so your next spring command will not have to reboot it. Ryan Bates compares spring with alternate preloaders zeus and commands in a recent RailsCast, but I’m finding spring to best fit my workflow.

xray-rails is some seriously spooky voodoo. You can think of it as the “Inspect Element” for your Rails view files. Toggling it on and off with a hot-key in the browser, it reveals the quadrants that represent all the various Ruby and Script templates on the screen. Clicking on one will open the relevant file in Sublime Text or whatever editor you set in ~/.xrayconfig (cough Emacs cough).

better_errors gives you a much more informative and navigable browser experience than the default Rails error page when things blow up. It is best combined with binder_of_caller which opens a full-blown interactive Ruby REPL at the point in your app’s code where things when wrong right there in the browser (!).

sextant is a tiny Rails engine that will output your application’s routes at the path /rails/routes, because no one has time to continually run rake routes. Luckily this is no longer needed in Rails 4.0+ as the core functionality has been merged into master.

letter_opener lets you preview an app sent email in a new browser tab instead of actually sending it. This is typically much more convenient for testing than wiring up SMTP settings and digging into your email client.

bullet is handy for sniffing out pesky performance-draining N+1 queries. You can configure it to print to your Rails development log, a dedicated bullet.log, your browser’s console, even send growl notifications or XMPP messages.

Having this ever-expanding tool-belt continually makes my Rails coding faster and more convenient. If you have any other development gem secret weapons, please leave a comment.