2010-10-08

Compass, Rails 3 and Heroku without a Hassle

If you are trying to use Compass with Rails 3 on Heroku and you are having no luck with Hassle, with some very quick hackitys, you can be on your merry way.

In config/application.rb, add this line to the end:

Sass::Plugin.options[:template_location] = { 'app/stylesheets' => 'tmp/stylesheets' }

In config/compass.rb, replace:

css_dir = "public/stylesheets"

with:

css_dir = "tmp/stylesheets"

In config/initializers/compass.rb, add this AssetTagHelper to serve stylesheets from tmp/ if they don’t exist in public/:

require 'fileutils'
FileUtils.mkdir_p(Rails.root.join('tmp', 'stylesheets'))

# a hackity way to get cache busting for stylesheets (rails_asset_id)
# try the original ("public/") path and if not found, try "tmp/".
module ActionView::Helpers::AssetTagHelper
  private
  def rails_asset_id_with_tmp_path(source)
    asset_id = rails_asset_id_without_tmp_path source
    if asset_id.blank?
      org_dir = config.assets_dir
      begin
        config.assets_dir = File.join(Rails.root, 'tmp')
        asset_id = rails_asset_id_without_tmp_path source
      ensure
        config.assets_dir = org_dir
      end
    end
    asset_id
  end
  alias_method_chain :rails_asset_id, :tmp_path
end

And, last but not least, in your config.ru, above your run Foo::Application, add:

use Rack::Static, :urls => ['/stylesheets'], :root => 'tmp'

Thanks to @jasoncodes for engineering these brilliant hackitys.

Tags: compass heroku no-hassle rails-3 18 notes View comments

  1. lucas-todayilearned posted this
Blog comments powered by Disqus