Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Install Sass on Heroku plugin.

  • Loading branch information...
commit fdbe2c6dae765030469bc76f79e2330248199a42 1 parent 6c1b096
@metaskills authored
View
1  app/views/layouts/application.html.haml
@@ -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
View
7 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 :)
View
3  vendor/plugins/sass_on_heroku/init.rb
@@ -0,0 +1,3 @@
+if RAILS_ENV == 'production'
+ ActionController::Dispatcher.middleware.use SassOnHeroku
+end
View
74 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
Please sign in to comment.
Something went wrong with that request. Please try again.