Created
May 25, 2011 19:39
-
-
Save agibralter/991732 to your computer and use it in GitHub Desktop.
Using Jammit and asset_hash with Rails 2.3.11 (Also, using my fork of asset_hash to avoid a Rails 3.x `require` conflict: https://github.com/agibralter/asset_hash)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/asset_fingerprints.rb -- A hack to get asset_hash to play nice with Rails 2.3.11 (since there is no ActionController::Base.asset_path configuration option). | |
# Same as deployment nginx configuration. | |
# No font/woff because it is already compressed: | |
# http://www.pixelastic.com/blog/87:gzipping-your-font-files | |
AssetHash::Base.gzip_types = %w( | |
application/javascript | |
application/json | |
application/xml | |
application/rss+xml | |
application/vnd.ms-fontobject | |
image/svg+xml | |
font/truetype | |
font/opentype | |
text/css | |
text/javascript | |
text/plain | |
text/x-component | |
text/xml | |
) | |
AssetHash::Base.asset_paths = %w( | |
favicon.ico | |
images | |
javascripts | |
stylesheets | |
assets | |
fonts | |
) | |
module AssetFingerprints | |
@@cache = {} | |
@@cache_guard = Mutex.new | |
def self.included(base) | |
base.class_eval do | |
alias_method_chain :rewrite_asset_path, :fingerprints | |
end | |
end | |
def rewrite_asset_path_with_fingerprints(source) | |
if !@@cache[source] | |
@@cache_guard.synchronize do | |
@@cache[source] = AssetHash.fingerprint(source) | |
end | |
end | |
@@cache[source] | |
end | |
end | |
ActionView::Helpers::AssetTagHelper.class_eval do | |
include AssetFingerprints | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/tasks/assets.rake | |
namespace :asset do | |
desc 'Process css and replace urls with deployment-ready urls.' | |
task :process_css => :environment do | |
Dir.glob(File.join(Rails.root, 'public', 'assets', '*.css')) do |path| | |
File.open(path, 'r+') do |f| | |
contents = f.read | |
contents.gsub!(/url\((?![^\)]*data:)(['"])?(.*?)(['"])?\)/) do |match| | |
q1 = $1 | |
path = $2 | |
q2 = $3 | |
path.gsub!(/\?\d+/, '') | |
# Some SVG fonts require the "#..." hash at the end of the url... preserve it. | |
path.gsub!(/(#\w+)/, '') | |
hash = $1 | |
path = AssetHash.fingerprint(path) | |
asset_host = "//#{ASSET_HOST}" | |
"url(#{q1}#{asset_host}#{path}#{hash}#{q2})" | |
end | |
f.rewind | |
f.truncate(contents.size) | |
f << contents | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/deploy/before_restart.rb | |
# CHEF deploy hook | |
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), "..", "..")) | |
def run_or_else(command) | |
command = "bin/#{command}" | |
system(command) or raise("before_restart: #{command} failed") | |
end | |
Dir.chdir(APP_ROOT) do | |
run_or_else("compass compile --force") | |
run_or_else("jammit") | |
run_or_else("rake asset:process_css") | |
run_or_else("rake asset:hash:generate") | |
run_or_else("rake god:running_start") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/environment.rb | |
RAILS_GEM_VERSION = '2.3.11' unless defined? RAILS_GEM_VERSION | |
require File.join(File.dirname(__FILE__), 'boot') | |
Rails::Initializer.run do |config| | |
# ... | |
config.after_initialize do | |
# ... | |
ActionController::Base.asset_host = Proc.new do |source, request| | |
"http#{'s' if request.ssl?}://#{ASSET_HOST}" | |
end | |
if Rails.env.staging? || Rails.env.production? | |
require 'asset_fingerprints' | |
end | |
# ... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment