I work on extensions (plugins) for Spree quite often. Extensions are Rails engines and thus, don’t have access to the complete suite of rake tasks that rails gives you. Recently, I needed to write tests for an extension that included a Backbone.js application written in Coffeescript. Let me first explain my dilemma, and then how I went about solving it.
If my extension was a complete rails app, I’d be able to use a gem like jasminerice to handle testing my coffeescript with Jasmine. Unfortunately, I can’t guarantee the presence of a live rails app to test against with this extension. I could test against the dummy app common in Rails engines, but using jasminerice with it would require some modifications to the dummy app itself. Rather than dive into the reasons why the dummy app didn’t fit my need, I’ll just skip straight to the solution.
So here are the two techniques I came up with:
Manually Run a Rake Task
First up, is a rake task I can run to manually precompile the assets for the engine to public/application.min.js. The following rake task can be added to your Rakefile and run with rake assets:compile.
The important line to note in the above rake task is
asset = sprockets['application.js']. You’ll want to make sure to point to your manifest file in that line, so Sprockets will know which files to precompile and in what order to precompile them.
The above rake task is also available in a gist.
Use guard-sprockets to Automatically Precompile Assets
The second method I came up with was to use guard-sprockets to automatically monitor my assets directory and precompile whenever I changed any of my application’s coffeescript.
Here’s how I configured my Guardfile:
The above Guardfile is also available in a gist.