Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow asset tag helper methods to accept :digest => false option in o…
…rder to completely avoid the digest generation.
  • Loading branch information
spastorino committed Sep 14, 2011
1 parent 49476ee commit da7f042
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 39 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG
Expand Up @@ -48,6 +48,10 @@


*Rails 3.1.1 (unreleased)* *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] * 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] * Set relative url root in assets when controller isn't available for Sprockets (eg. Sass files using asset_path). Fixes #2435 [Guillermo Iguaran]
Expand Down
9 changes: 5 additions & 4 deletions actionpack/lib/action_view/asset_paths.rb
Expand Up @@ -21,14 +21,15 @@ def initialize(config, controller = nil)
# When :relative (default), the protocol will be determined by the client using current protocol # When :relative (default), the protocol will be determined by the client using current protocol
# When :request, the protocol will be the request protocol # When :request, the protocol will be the request protocol
# Otherwise, the protocol is used (E.g. :http, :https, etc) # 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 source = source.to_s
return source if is_uri?(source) return source if is_uri?(source)


source = rewrite_extension(source, dir, ext) if ext options[:include_host] ||= true
source = rewrite_asset_path(source, dir) 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_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 source
end end


Expand Down
Expand Up @@ -60,8 +60,8 @@ def include_tag(*sources)


private private


def path_to_asset(source, include_host = true, protocol = nil) def path_to_asset(source, options = {})
asset_paths.compute_public_path(source, asset_name.to_s.pluralize, extension, include_host, protocol) asset_paths.compute_public_path(source, asset_name.to_s.pluralize, options.merge(:ext => extension))
end end


def path_to_asset_source(source) def path_to_asset_source(source)
Expand Down
Expand Up @@ -41,7 +41,7 @@ def rewrite_extension(source, dir, ext)


# Break out the asset path rewrite in case plugins wish to put the asset id # Break out the asset path rewrite in case plugins wish to put the asset id
# someplace other than the query string. # 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] == ?/ source = "/#{dir}/#{source}" unless source[0] == ?/
path = config.asset_path path = config.asset_path


Expand Down
Expand Up @@ -83,7 +83,7 @@ def register_javascript_expansion(expansions)
# javascript_path "http://www.example.com/js/xmlhr" # => http://www.example.com/js/xmlhr # 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 # javascript_path "http://www.example.com/js/xmlhr.js" # => http://www.example.com/js/xmlhr.js
def javascript_path(source) def javascript_path(source)
asset_paths.compute_public_path(source, 'javascripts', 'js') asset_paths.compute_public_path(source, 'javascripts', :ext => 'js')
end end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route


Expand Down
Expand Up @@ -17,7 +17,7 @@ def extension


def asset_tag(source, options) def asset_tag(source, options)
# We force the :request protocol here to avoid a double-download bug in IE7 and IE8 # 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 end


def custom_dir def custom_dir
Expand Down Expand Up @@ -61,7 +61,7 @@ def register_stylesheet_expansion(expansions)
# stylesheet_path "http://www.example.com/css/style" # => http://www.example.com/css/style # 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 # stylesheet_path "http://www.example.com/css/style.css" # => http://www.example.com/css/style.css
def stylesheet_path(source) 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 end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route


Expand Down
28 changes: 15 additions & 13 deletions actionpack/lib/sprockets/helpers/rails_helper.rb
Expand Up @@ -25,38 +25,40 @@ def javascript_include_tag(*sources)
options = sources.extract_options! options = sources.extract_options!
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets? debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false body = options.key?(:body) ? options.delete(:body) : false
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?


sources.collect do |source| sources.collect do |source|
if debug && asset = asset_paths.asset_for(source, 'js') if debug && asset = asset_paths.asset_for(source, 'js')
asset.to_a.map { |dep| 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 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
end.join("\n").html_safe end.join("\n").html_safe
end end


def stylesheet_link_tag(*sources) def stylesheet_link_tag(*sources)
options = sources.extract_options! options = sources.extract_options!
debug = options.key?(:debug) ? options.delete(:debug) : debug_assets? debug = options.key?(:debug) ? options.delete(:debug) : debug_assets?
body = options.key?(:body) ? options.delete(:body) : false body = options.key?(:body) ? options.delete(:body) : false
digest = options.key?(:digest) ? options.delete(:digest) : digest_assets?


sources.collect do |source| sources.collect do |source|
if debug && asset = asset_paths.asset_for(source, 'css') if debug && asset = asset_paths.asset_for(source, 'css')
asset.to_a.map { |dep| 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 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
end.join("\n").html_safe end.join("\n").html_safe
end 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) source = source.logical_path if source.respond_to?(:logical_path)
path = asset_paths.compute_public_path(source, 'assets', default_ext, true, protocol) path = asset_paths.compute_public_path(source, 'assets', options.merge(:body => true))
body ? "#{path}?body=1" : path options[:body] ? "#{path}?body=1" : path
end end


private private
Expand Down Expand Up @@ -100,8 +102,8 @@ class AssetPaths < ::ActionView::AssetPaths #:nodoc:


class AssetNotPrecompiledError < StandardError; end class AssetNotPrecompiledError < StandardError; end


def compute_public_path(source, dir, ext = nil, include_host = true, protocol = nil) def compute_public_path(source, dir, options = {})
super(source, asset_prefix, ext, include_host, protocol) super(source, asset_prefix, options)
end end


# Return the filesystem path for the source # Return the filesystem path for the source
Expand Down Expand Up @@ -131,11 +133,11 @@ def digest_for(logical_path)
end end
end end


def rewrite_asset_path(source, dir) def rewrite_asset_path(source, dir, options = {})
if source[0] == ?/ if source[0] == ?/
source source
else else
source = digest_for(source) source = digest_for(source) unless options[:digest] == false
source = File.join(dir, source) source = File.join(dir, source)
source = "/#{source}" unless source =~ /^\// source = "/#{source}" unless source =~ /^\//
source source
Expand Down
44 changes: 28 additions & 16 deletions actionpack/test/template/sprockets_helper_test.rb
Expand Up @@ -41,6 +41,10 @@ def url_for(*args)
test "asset_path" do test "asset_path" do
assert_match %r{/assets/logo-[0-9a-f]+.png}, assert_match %r{/assets/logo-[0-9a-f]+.png},
asset_path("logo.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 end


test "asset_path with root relative assets" do test "asset_path with root relative assets" do
Expand Down Expand Up @@ -133,25 +137,29 @@ def url_for(*args)


test "javascript path" do test "javascript path" do
assert_match %r{/assets/application-[0-9a-f]+.js}, 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}, 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}, 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", assert_equal "/dir/xmlhr.js",
asset_path("/dir/xmlhr", "js") asset_path("/dir/xmlhr", :ext => "js")


assert_equal "http://www.example.com/js/xmlhr", 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", 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 end


test "javascript include tag" do test "javascript include tag" do
assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>}, assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag(:application) 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>}, assert_match %r{<script src="/assets/xmlhr-[0-9a-f]+.js" type="text/javascript"></script>},
javascript_include_tag("xmlhr") javascript_include_tag("xmlhr")
Expand All @@ -173,21 +181,25 @@ def url_for(*args)
end end


test "stylesheet path" do 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/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", "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", "css") assert_equal "/dir/style.css", asset_path("/dir/style.css", :ext => "css")


assert_equal "http://www.example.com/css/style", 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", 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 end


test "stylesheet link tag" do test "stylesheet link tag" do
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />}, assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag(:application) 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" />}, assert_match %r{<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
stylesheet_link_tag("style") stylesheet_link_tag("style")
Expand Down Expand Up @@ -218,25 +230,25 @@ def url_for(*args)


test "alternate asset prefix" do test "alternate asset prefix" do
stubs(:asset_prefix).returns("/themes/test") 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 end


test "alternate asset environment" do test "alternate asset environment" do
assets = Sprockets::Environment.new assets = Sprockets::Environment.new
assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets")) assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets"))
stubs(:asset_environment).returns(assets) 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 end


test "alternate hash based on environment" do test "alternate hash based on environment" do
assets = Sprockets::Environment.new assets = Sprockets::Environment.new
assets.version = 'development' assets.version = 'development'
assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets")) assets.append_path(FIXTURES.join("sprockets/alternate/stylesheets"))
stubs(:asset_environment).returns(assets) stubs(:asset_environment).returns(assets)
dev_path = asset_path("style", "css") dev_path = asset_path("style", :ext => "css")


assets.version = 'production' assets.version = 'production'
prod_path = asset_path("style", "css") prod_path = asset_path("style", :ext => "css")


assert_not_equal prod_path, dev_path assert_not_equal prod_path, dev_path
end end
Expand Down

0 comments on commit da7f042

Please sign in to comment.