Skip to content

Instantly share code, notes, and snippets.

@dblock
Last active September 18, 2018 03:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dblock/1451946 to your computer and use it in GitHub Desktop.
Save dblock/1451946 to your computer and use it in GitHub Desktop.
A monkey patch for serving compressed assets for Rails 3.0 (asset_tag_helper.rb) and another for Rails 3.1 (asset_paths.rb).
# Rails 4
require 'action_view/helpers/asset_url_helper'
module ActionView
module Helpers
module AssetUrlHelper
def accept_encoding?(encoding)
request = self.request if respond_to?(:request)
return false unless request
(request.env['HTTP_ACCEPT_ENCODING'] || '').split(',').include?(encoding)
end
def rewrite_path_to_gzip?(source, options)
request = self.request if respond_to?(:request)
return false unless request
[ :javascript, :stylesheet ].include?(options[:type]) &&
Rails.application.asset_url(request.try(:scheme) || 'http').present? &&
accept_encoding?('gzip')
end
alias_method :_asset_path, :asset_path
def asset_path(source, options = {})
_asset_path(source, options).tap do |path|
path << '.cgz' if rewrite_path_to_gzip?(source, options)
end
end
alias_method :path_to_asset, :asset_path
end
end
end
# Rails 3
module ActionView
class AssetPaths
def accept_encoding?(encoding)
return false unless current_request
(current_request.env['HTTP_ACCEPT_ENCODING'] || '').split(',').include?(encoding)
end
def rewrite_path_to_gzip?(source, options)
[ "js", "css" ].include?(options[:ext]) and
(! config.asset_host.blank?) and
(source =~ /assets\//) and
accept_encoding?('gzip')
end
def rewrite_path_to_gzip(source)
source + ".cgz"
end
alias_method :actionview_assetpaths_compute_public_path, :compute_public_path
def compute_public_path(source, dir, options = {})
source = rewrite_path_to_gzip(source) if rewrite_path_to_gzip?(source, options)
actionview_assetpaths_compute_public_path(source, dir, options)
end
end
end
# Rails 3
module ActionView
module Helpers
module AssetTagHelper
def accept_encoding?(encoding)
(request.env['HTTP_ACCEPT_ENCODING'] || '').split(',').include?(encoding)
end
def rewrite_path_to_gzip?(source)
(! config.asset_host.blank?) and (source =~ /assets\//) and accept_encoding?('gzip')
end
def path_to_javascript(source)
source = rewrite_path_to_gzip(source) if rewrite_path_to_gzip?(source)
compute_public_path(source, 'javascripts', 'js')
end
def path_to_stylesheet(source)
source = rewrite_path_to_gzip(source) if rewrite_path_to_gzip?(source)
compute_public_path(source, 'stylesheets', 'css')
end
def rewrite_path_to_gzip(source)
source + ".cgz"
end
end
end
end
# Rails 3
require 'spec_helper'
describe ActionView::Helpers::AssetTagHelper do
before(:each) do
@helper = SpecAssetTagHelper.new
end
context "accept_encoding?" do
it "works for an empty string" do
@helper.accept_encoding?('gzip').should be_false
end
it "works for one encoding that is not accepted" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'foo'
@helper.accept_encoding?('gzip').should be_false
end
it "works for two encodings that are not accepted" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'foo,bar'
@helper.accept_encoding?('gzip').should be_false
end
it "works for one encoding that is accepted" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
@helper.accept_encoding?('gzip').should be_true
end
it "works for two encodings when one is accepted" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'foo,gzip'
@helper.accept_encoding?('gzip').should be_true
end
end
context "rewrite_path_to_gzip?" do
it "works for no headers and a non-asset path" do
@helper.rewrite_path_to_gzip?('foo').should be_false
end
it "doesn't rewrite non-asset paths" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
@helper.rewrite_path_to_gzip?('server/bar/foo.css').should be_false
end
it "rewrites asset paths" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
@helper.rewrite_path_to_gzip?('server/assets/foo.css').should be_true
end
end
context "path_to_javascript" do
it "rewrites asset path" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
@helper.path_to_javascript('/assets/javascripts/example.js').should == "http://stagic.example.com/assets/javascripts/example.js.cgz"
end
end
context "path_to_stylesheet" do
it "rewrites asset path" do
@helper.env['HTTP_ACCEPT_ENCODING'] = 'gzip'
@helper.path_to_stylesheet('/assets/stylesheets/example.css').should == "http://stagic.example.com/assets/stylesheets/example.css.cgz"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment