Skip to content

Instantly share code, notes, and snippets.

@dpoggi
Created March 27, 2011 05:59
Show Gist options
  • Save dpoggi/888962 to your computer and use it in GitHub Desktop.
Save dpoggi/888962 to your computer and use it in GitHub Desktop.
AssetsController. See README.txt.
Assets controller for caching stylesheets and JavaScripts when deploying to Heroku or another read-only FS.
Stylesheets go in app/stylesheets/package/*.scss, JavaScripts go in app/javascripts/package/*.js.
Recommended packages are below, change them as you see fit - just make sure the folders exist.
Original implementation used Dir.glob to get all the files from a package folder,
but that soon revealed itself to be a folly as order is important with these things.
To include one of these packages in a view, use:
<%= cached_stylesheet_tag 'package_name', :media => 'media_type' %>
<%= cached_javascript_tag 'package_name' %>
for ERB, or
= cached_stylesheet_tag 'package_name', :media => 'media_type'
= cached_javascript_tag 'package_name'
for Haml. Both helpers pass all options along to the normal Rails helpers.
Thanks to this article for... everything:
http://avandamiri.com/2010/09/15/managing-styles-with-sass-on-heroku.html
def most_recent_date_in_folder(folder)
Dir.glob(folder + '/*').map {|file| File.mtime(file).to_i}.max
end
def cached_stylesheet_tag(package, options = {})
last_modified_date = most_recent_date_in_folder "#{Rails.root}/app/stylesheets/#{package}"
full_css_path = stylesheet_path(package) + "?" + last_modified_date.to_s
yield full_css_path if block_given?
stylesheet_link_tag full_css_path, options
end
def cached_javascript_tag(package, options = {})
last_modified_date = most_recent_date_in_folder "#{Rails.root}/app/javascripts/#{package}"
full_js_path = javascript_path(package) + "?" + last_modified_date.to_s
yield full_js_path if block_given?
javascript_include_tag full_js_path, options
end
class AssetsController < ApplicationController
def index
@output = ""
package = params[:package].to_sym
css_packages = {
:screen => [],
:print => [],
:ie => []
}
js_packages = {
:site => []
}
case params[:type]
when :css
if not css_packages[package].nil?
sass_options = {:syntax => :scss, :style => Rails.env.development? ? :compact : :compressed}
css_packages[package].map! {|sheet| "#{Rails.root}/app/stylesheets/#{params[:package]}/#{sheet}.scss"}
css_packages[package].each {|file| @output += Sass::Engine.new(File.open(file, 'r').read, sass_options).render}
end
response.content_type = 'text/css'
when :js
if not js_packages[package].nil?
js_packages[package].map! {|script| "#{Rails.root}/app/javascripts/#{params[:package]}/#{script}.js"}
js_packages[package].each {|file| @output += File.open(file, 'r').read}
end
response.content_type = 'text/javascript'
end
response.headers['Cache-Control'] = "public, max-age=#{1.year.seconds.to_i}" unless Rails.env.development?
render :text => @output
end
end
gem 'haml'
match '/stylesheets/:package.css', :to => 'assets#index', :as => 'stylesheet', :type => :css
match '/javascripts/:package.js', :to => 'assets#index', :as => 'javascript', :type => :js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment