Smart Asset Management for Rails Plugins

Posted by Matthew Bass on Nov 14th, 2008

Many Rails plugins require that certain files like images, CSS, and JavaScript get copied to the public folder during installation. The typical way to do this is add code to install.rb, which then gets executed when you first install the plugin in your Rails project. What happens when the assets in a plugin you’re using change, though? You end up having to manually copy files around anytime the plugin gets updated, which is an extremely error-prone process (if you remember to do it in the first place!).

I got tired of mucking around with install.rb and having to sling files around whenever a plugin changed, and so I wrote asset_copier. It’s a generator that runs against your plugin and installs an asset management system that gets checked in with the rest of your plugin code. Once installed, any Rails app your plugin is installed in gets a plugin_name:install task that intelligently copies over any files located in the /files subdirectory in the plugin. Files will also be copied over every time the plugin is reloaded or Rails is restarted in development mode. When running in production mode, warnings will be printed to the log for files that are missing or have changed.

You can check out the README for installation instructions and additional documentation, and I’d love to get any and all feedback you have as you start using it in your own plugins.

Oh, and if you’re looking for a plugin using asset_copier, look no further than the textile_toolbar that Terralien announced recently.

You can still contact Nathaniel at nathaniel@terralien.com