Nithin Bekal

Posts About Notes Slides

Better Asset Management in Rails Using Rails Assets

10 Nov 2014

I’ve been looking for better ways to manage the JavaScript asset dependencies in a Rails app. It’s easy to find gems that package many client side JS libraries, but a lot of them are unmaintained. It becomes a pain when you’re trying to upgrade Rails, but are blocked by these gems.

Bower is a package manager for such assets, and would have solved this problem. But I was reluctant to add another package manager to the mix and make the deployment setup even more complex.

That’s when I came across rails-assets, which is a proxy between bundler and bower. This allows us to include bower packages in our application using only bundler. You don’t need to have bower installed on your machine.

To use it, you need to add source 'https://rails-assets.org' at the top of your Gemfile, and then include any bower packages as gems with rails-assets- prefixed to their name.

For example, you could add gem 'rails-assets-bootstrap' and you will be able to accesss bootstrap assets from application.js (using //= require bootstrap) and application.css (using *= require bootstrap).

When you request a gem from rails-assets, it converts bower packages into gems that can be installed via bundler. I’ve only used it with Rails, but it is compatible with any sprockets based application.

The project has been around for more than a year, and it seems to be stable. Although I haven’t yet moved to rails-assets for all the client side assets in my app, this has already simplified the asset dependencies a great deal.

Links

Hi, I’m Nithin Bekal. I work at Shopify in Ottawa, Canada. Previously, co-founder of CrowdStudio.in and WowMakers. Ruby is my preferred programming language, and the topic of most of my articles here, but I'm also a big fan of Elixir. Tweet to me at @nithinbekal.