1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow asset tag helper methods to accept :digest => false option in order to completely avoid the digest generation.

This commit is contained in:
Santiago Pastorino 2011-09-14 14:47:49 -07:00
parent 49476eee78
commit da7f0426ec
8 changed files with 58 additions and 39 deletions

View file

@ -48,6 +48,10 @@
*Rails 3.1.1 (unreleased)*
* Allow asset tag helper methods to accept :digest => false option in order to completely avoid the digest generation.
Useful for linking assets from static html files or from emails when the user
could probably look at an older html email with an older asset. [Santiago Pastorino]
* Don't mount Sprockets server at config.assets.prefix if config.assets.compile is false. [Mark J. Titorenko]
* Set relative url root in assets when controller isn't available for Sprockets (eg. Sass files using asset_path). Fixes #2435 [Guillermo Iguaran]

View file

@ -21,14 +21,15 @@ module ActionView
# When :relative (default), the protocol will be determined by the client using current protocol
# When :request, the protocol will be the request protocol
# Otherwise, the protocol is used (E.g. :http, :https, etc)
def compute_public_path(source, dir, ext = nil, include_host = true, protocol = nil)
def compute_public_path(source, dir, options = {})
source = source.to_s
return source if is_uri?(source)
source = rewrite_extension(source, dir, ext) if ext
source = rewrite_asset_path(source, dir)
options[:include_host] ||= true
source = rewrite_extension(source, dir, options[:ext]) if options[:ext]
source = rewrite_asset_path(source, dir, options)
source = rewrite_relative_url_root(source, relative_url_root)
source = rewrite_host_and_protocol(source, protocol) if include_host
source = rewrite_host_and_protocol(source, options[:protocol]) if options[:include_host]
source
end

View file

@ -60,8 +60,8 @@ module ActionView
private
def path_to_asset(source, include_host = true, protocol = nil)
asset_paths.compute_public_path(source, asset_name.to_s.pluralize, extension, include_host, protocol)
def path_to_asset(source, options = {})
asset_paths.compute_public_path(source, asset_name.to_s.pluralize, options.merge(:ext => extension))
end
def path_to_asset_source(source)

View file

@ -41,7 +41,7 @@ module ActionView
# Break out the asset path rewrite in case plugins wish to put the asset id
# someplace other than the query string.
def rewrite_asset_path(source, dir)
def rewrite_asset_path(source, dir, options = nil)
source = "/#{dir}/#{source}" unless source[0] == ?/
path = config.asset_path

View file

@ -83,7 +83,7 @@ module ActionView
# javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr
# javascript_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
def javascript_path(source)
asset_paths.compute_public_path(source, 'javascripts', 'js')
asset_paths.compute_public_path(source, 'javascripts', :ext => 'js')
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route

View file

@ -17,7 +17,7 @@ module ActionView
def asset_tag(source, options)
# We force the :request protocol here to avoid a double-download bug in IE7 and IE8
tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source, true, :request)) }.merge(options), false, false)
tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => ERB::Util.html_escape(path_to_asset(source, :protocol => :request)) }.merge(options), false, false)
end
def custom_dir
@ -61,7 +61,7 @@ module ActionView
# stylesheet_path "http://www.example.com/css/style" # => http://www.example.com/css/style
# stylesheet_path "http://www.example.com/css/style.css" # => http://www.example.com/css/style.css
def stylesheet_path(source)
asset_paths.compute_public_path(source, 'stylesheets', 'css', true, :request)
asset_paths.compute_public_path(source, 'stylesheets', :ext => 'css', :protocol => :request)
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route

View file

@ -25,38 +25,40 @@ module Sprockets
options = sources.extract_options!
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
sources.collect do |source|
if debug && asset = asset_paths.asset_for(source, 'js')
asset.to_a.map { |dep|
super(dep.to_s, { :src => asset_path(dep, 'js', true) }.merge!(options))
super(dep.to_s, { :src => asset_path(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
}
else
super(source.to_s, { :src => asset_path(source, 'js', body) }.merge!(options))
super(source.to_s, { :src => asset_path(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
end
end.join("\n").html_safe
end
def stylesheet_link_tag(*sources)
options = sources.extract_options!
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?
sources.collect do |source|
if debug && asset = asset_paths.asset_for(source, 'css')
asset.to_a.map { |dep|
super(dep.to_s, { :href => asset_path(dep, 'css', true, :request) }.merge!(options))
super(dep.to_s, { :href => asset_path(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
}
else
super(source.to_s, { :href => asset_path(source, 'css', body, :request) }.merge!(options))
super(source.to_s, { :href => asset_path(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
end
end.join("\n").html_safe
end
def asset_path(source, default_ext = nil, body = false, protocol = nil)
def asset_path(source, options = {})
source = source.logical_path if source.respond_to?(:logical_path)
path = asset_paths.compute_public_path(source, 'assets', default_ext, true, protocol)
body ? "#{path}?body=1" : path
path = asset_paths.compute_public_path(source, 'assets', options.merge(:body => true))
options[:body] ? "#{path}?body=1" : path
end
private
@ -100,8 +102,8 @@ module Sprockets
class AssetNotPrecompiledError < StandardError; end
def compute_public_path(source, dir, ext = nil, include_host = true, protocol = nil)
super(source, asset_prefix, ext, include_host, protocol)
def compute_public_path(source, dir, options = {})
super(source, asset_prefix, options)
end
# Return the filesystem path for the source
@ -131,11 +133,11 @@ module Sprockets
end
end
def rewrite_asset_path(source, dir)
def rewrite_asset_path(source, dir, options = {})
if source[0] == ?/
source
else
source = digest_for(source)
source = digest_for(source) unless options[:digest] == false
source = File.join(dir, source)
source = "/#{source}" unless source =~ /^\//
source

View file

@ -41,6 +41,10 @@ class SprocketsHelperTest < ActionView::TestCase
test "asset_path" do
assert_match %r{/assets/logo-[0-9a-f]+.png},
asset_path("logo.png")
assert_match %r{/assets/logo-[0-9a-f]+.png},
asset_path("logo.png", :digest => true)
assert_match %r{/assets/logo.png},
asset_path("logo.png", :digest => false)
end
test "asset_path with root relative assets" do
@ -133,25 +137,29 @@ class SprocketsHelperTest < ActionView::TestCase
test "javascript path" do
assert_match %r{/assets/application-[0-9a-f]+.js},
asset_path(:application, "js")
asset_path(:application, :ext => "js")
assert_match %r{/assets/xmlhr-[0-9a-f]+.js},
asset_path("xmlhr", "js")
asset_path("xmlhr", :ext => "js")
assert_match %r{/assets/dir/xmlhr-[0-9a-f]+.js},
asset_path("dir/xmlhr.js", "js")
asset_path("dir/xmlhr.js", :ext => "js")
assert_equal "/dir/xmlhr.js",
asset_path("/dir/xmlhr", "js")
asset_path("/dir/xmlhr", :ext => "js")
assert_equal "http://www.example.com/js/xmlhr",
asset_path("http://www.example.com/js/xmlhr", "js")
asset_path("http://www.example.com/js/xmlhr", :ext => "js")
assert_equal "http://www.example.com/js/xmlhr.js",
asset_path("http://www.example.com/js/xmlhr.js", "js")
asset_path("http://www.example.com/js/xmlhr.js", :ext => "js")
end
test "javascript include tag" do
assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag(:application)
assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag(:application, :digest => true)
assert_match %r{<script src="/assets/application.js" type="text/javascript"></script>},
javascript_include_tag(:application, :digest => false)
assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag("xmlhr")
@ -173,21 +181,25 @@ class SprocketsHelperTest < ActionView::TestCase
end
test "stylesheet path" do
assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, "css")
assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :ext => "css")
assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", "css")
assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", "css")
assert_equal "/dir/style.css", asset_path("/dir/style.css", "css")
assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", :ext => "css")
assert_equal "/dir/style.css", asset_path("/dir/style.css", :ext => "css")
assert_equal "http://www.example.com/css/style",
asset_path("http://www.example.com/css/style", "css")
asset_path("http://www.example.com/css/style", :ext => "css")
assert_equal "http://www.example.com/css/style.css",
asset_path("http://www.example.com/css/style.css", "css")
asset_path("http://www.example.com/css/style.css", :ext => "css")
end
test "stylesheet link tag" do
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application)
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :digest => true)
assert_match %r{<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application, :digest => false)
assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style")
@ -218,14 +230,14 @@ class SprocketsHelperTest < ActionView::TestCase
test "alternate asset prefix" do
stubs(:asset_prefix).returns("/themes/test")
assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", "css")
assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
end
test "alternate asset environment" do
assets = Sprockets::Environment.new
assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets"))
stubs(:asset_environment).returns(assets)
assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", "css")
assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
end
test "alternate hash based on environment" do
@ -233,10 +245,10 @@ class SprocketsHelperTest < ActionView::TestCase
assets.version = 'development'
assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets"))
stubs(:asset_environment).returns(assets)
dev_path = asset_path("style", "css")
dev_path = asset_path("style", :ext => "css")
assets.version = 'production'
prod_path = asset_path("style", "css")
prod_path = asset_path("style", :ext => "css")
assert_not_equal prod_path, dev_path
end