Skip to content

Instantly share code, notes, and snippets.

@patio11
Created November 30, 2010 03:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patio11/721114 to your computer and use it in GitHub Desktop.
Save patio11/721114 to your computer and use it in GitHub Desktop.
#This code is MIT licensed.
module ActionView
module Helpers
module AssetTagHelper
require 'jsminlib' #Google it. Paste into RAILS_ROOT/libs. Note: not MIT license (has a "This code should be used for good, not evil" rider -- ask your lawyers if they care).
def compress_css(source)
source.gsub!(/\s+/, " ") # collapse space
source.gsub!(/\/\*(.*?)\*\/ /, "") # remove comments
source.gsub!(/\} /, "}\n") # add line breaks
source.gsub!(/\n$/, "") # remove last break
source.gsub!(/ \{ /, " {") # trim inside brackets
source.gsub!(/; \}/, "}") # trim inside brackets
source.gsub!("http://", "https://") if request.ssl? #Causes background images linked in CSS files to automatically go to HTTPS if those CSS files are cached from HTTPS requests. You should probably use :cache => request.ssl? ? "secure-foo" : "standard-foo" so that the proper cache is used for each kind of request in the future.
source
end
def get_file_contents(filename)
contents = File.read(filename)
if filename =~ /\.js$/
JSMin.minimize(contents)
elsif filename =~ /\.css$/
compress_css(contents)
end
end
def join_asset_file_contents(paths)
paths.collect { |path|
get_file_contents(File.join(ASSETS_DIR, path.split("?").first)) }.join("\n\n")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment