Skip to content

Commit

Permalink
Install Sass on Heroku plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
metaskills committed Jan 21, 2010
1 parent 6c1b096 commit fdbe2c6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
1 change: 0 additions & 1 deletion app/views/layouts/application.html.haml
Expand Up @@ -8,7 +8,6 @@
= stylesheet_link_tag :all, :recursive => true
= javascript_include_tag 'prototype', 'effects', 'builder', 'application'
= javascript_tag 'Builder.dump();'
%script{:src => '/mint/index.php?js', :type => 'text/javascript'}

%body

Expand Down
7 changes: 7 additions & 0 deletions vendor/plugins/sass_on_heroku/README.rdoc
@@ -0,0 +1,7 @@
= Sass on Heroku

Rails plugin to compile Sass files to tmp/ and serve from there, avoiding trouble with read-only filesystems.

It will add a Rack middleware on top of your Rails app that quickly detects requests for Sass CSS files, and serve them with caching headers.

If you have issues please tell us on support.heroku.com -- or just fork it :)
3 changes: 3 additions & 0 deletions vendor/plugins/sass_on_heroku/init.rb
@@ -0,0 +1,3 @@
if RAILS_ENV == 'production'
ActionController::Dispatcher.middleware.use SassOnHeroku
end
74 changes: 74 additions & 0 deletions vendor/plugins/sass_on_heroku/lib/sass_on_heroku.rb
@@ -0,0 +1,74 @@
class SassOnHeroku
def self.original_css_location
@@original_css_location ||= nil
end

def self.original_css_location=(location)
@@original_css_location = location
end

def initialize(app)
@app = app
initialize_sass unless self.class.original_css_location
end

def initialize_sass
# get the original location of compiled css files (ie: stylesheets/compiled)
# and replace it to a location in tmp
if Sass::Plugin.options[:template_location].is_a?(Hash)
self.class.original_css_location = Sass::Plugin.options[:template_location].values.first
Sass::Plugin.options[:template_location].keys.each do |k|
Sass::Plugin.options[:template_location][k] = css_location_on_heroku
end
else
self.class.original_css_location = Sass::Plugin.options[:css_location]
Sass::Plugin.options[:css_location] = css_location_on_heroku
end

# compile Sass once, and set it to not compile again
Sass::Plugin.options[:always_update] = true
Sass::Plugin.options[:never_update] = false
Sass::Plugin.update_stylesheets
Sass::Plugin.options[:never_update] = true
end

def call(env)
if !stylesheets.empty? && env['REQUEST_PATH'] =~ css_request_regexp
return render_sass($1)
end
@app.call(env)
end

def render_sass(name)
css_file = "#{css_location_on_heroku}/#{name}"
[
200,
{
'Cache-Control' => 'public, max-age=86400',
'Content-Length' => File.size(css_file).to_s,
'Content-Type' => 'text/css'
},
File.read(css_file)
]
end

def css_location_on_heroku
"#{RAILS_ROOT}/tmp/sass-output"
end

def stylesheets
@stylesheets ||= Dir[css_location_on_heroku + '/*.css'].map { |f| f.split('/').last }
end

def css_request_regexp
@css_request_regexp ||= build_regexp
end

# builds a regexp that matches requests for Sass stylesheets
# ie : \/stylesheets\/sass\/(file1\.css|file2\.css)
def build_regexp
files = stylesheets.map { |f| Regexp.escape(f) }
path = Regexp.escape(self.class.original_css_location.gsub("#{RAILS_ROOT}/public", '') + '/')
regexp = Regexp.new("^#{path}(" + files.join('|') + ')(\?.*)?$')
end
end

0 comments on commit fdbe2c6

Please sign in to comment.