Capistrano + Rails 3.1 assets can be tricky

The assets pipeline of Rails 3.1 is a nice thing when mastered, but it can also be tricky on some situations. And deployment can be one of those situations. Hopefully, capistrano has some included recipes for this. All you have to do, is adding this line to your config/deploy.rb;

load "deploy/assets"

But, I wanted more: to be able to compile assets with the digest generation, but also to use some CSS with a known name that I can include in some static files (the error pages in fact). I've not found an easy way to compile the assets two times, one with digest and the other without it, but it doesn't matter as the manifest.yaml gives me all the informations I needed.

This file is a YAML with an associative array that gives the computed filename with the digest for each asset. So, with it, I can create a symlink to make the CSS available without digest. It's done with this rule in config/deploy.rb:

namespace :assets do
  desc "Create a symlink for application.css (used by static pages)"
  task :static, :roles => :web, :except => { :no_release => true } do
    %w(application.css other.css).each do |asset|
      file = capture "cd #{shared_path}/assets && ruby -ryaml -e 'p YAML.load_file(\"manifest.yml\")[\"#{asset}\"]'"
      run "cd #{shared_path}/assets && ln -sf #{file.chomp} #{asset}"
after "assets:precompile", "assets:static"

What do you think of this solution?

blog comments powered by Disqus