Handy Rails Gems
At Tenex, Ruby on Rails is one of our favorite technologies. This is in part because of the vibrant ecosystem of high-quality Gems available that streamline development. Some of these gems are indeed applicable to nearly any project. Here’s a list of some of our favorites.
Complete mail-handling solution for development
Most web applications deal with email at some point, most often with user registration. Making that work in development can be tricky, especially when dealing with draconian firewalls forbidding common email ports. To further complicate things, many companies encourage developers to connect to a centralized “testing” database which is periodically copied from production. Of course this would have real user accounts and thus real email addresses of customers in it – do you really want to risk accidentally sending your user base those new email templates you’re writing? Mailcatcher solves this problem by running a local SMTP server combined with a web front-end that mimics a real mail system. Installation is very simple and it even runs on Windows. You don’t even add it to your Gemfile!
Easily use Bower packages from Rails
This one isn’t a gem, but rather an entire repository of gems! Manually downloading “vendor” assets, such as AngularJS or jQuery, is tedious and error-prone. Minification and caching are hampered by sidestepping the asset pipeline, not to mention that by manually copying such dependencies, you are effectively copying the entire “compiled” code of another repository into your own. Of course, Ruby uses Bundler, but your assets’ are likely packaged with Bower. Rails-Assets.org is the bridge across this gap. Here’s a snippet from one of our Gemfiles that loads Angular and some third party extensions directly into our asset pipeline:
source 'https://rails-assets.org' do gem 'rails-assets-angular' gem 'rails-assets-angular-bootstrap' gem 'rails-assets-angular-growl-2' # ... end
Then just add the JS and CSS to your manifest (or include them somewhere else) and they’ll be in your pipeline!
Full debugging environment right in your browser
Rails comes with a rather bland error page for development. Being able to graphically examine stack frames, their local variables, the controller’s parameters and even the Rack environment would be much more helpful. And that’s exactly what
better_errors provides, plus a REPL that you can use right from your browser!
rails console will bring you into an IRB session. With
pry-rails in your Gemfile, you will get a much improved REPL that features everything from colors to tab completion. Debugging with
pry is a very productive experience. If you want to find out exactly what’s going on in a controller action, for example, just pop in
binding.pry wherever you would like to set a breakpoint, hit the page you’re debugging, and your
rails server console will give you a
pry REPL session.
Store trees in relational databases
One of the most annoying examples of the impedence mismatch between relational databases and object-oriented code is encountered when trying to model a tree structure. A common but poor solution is an adjacency list, which simply adds one column to the table for a
parent_id – a foreign key to the same table. Faster solutions are hard to implement without proprietary SQL extensions. But fortunately, the authors of the
closure_tree gem have done this work for you! For details on why a Closure Tree is such a great solution, check out this slideshare and this coderwall.com post.
It paginates ActiveRecord relations without resorting to the more intrusive moneky-patching techniques of other pagination libraries, like
Private messaging system
If you need a private messaging system, check out Mailboxer. It’s easy to integrate with Griddler to allow users receiving notification emails about messages on your site to reply directly from their mail client.
Instant CRUD Application
Being able to CRUD (Create, Read, Update and Delete) any model in your project shouldn’t involve hundreds of lines of boilerplate, even if it is automatically generated. By installing
rails_admin, you get a standard CRUD interface for every model in your project with less than 5 minutes of work. The result can be customized to an extent, but if you’re looking to make more significant changes, you should check out Active Admin.
Reduce asset-related console spam
The development console yields all sorts of useful information. But sometimes that is lost in noise about your assets:
Started GET "/assets/application.js" for 127.0.0.1 at 2015-01-28 13:35:34 +0300 Served asset /application.js - 304 Not Modified (8ms)
quiet_assets does what it says on the box!