Switch to asset_path and make it available in the Sprockets::Context (now you...

Switch to asset_path and make it available in the Sprockets::Context (now you can do asset_path("logo.png") in a stylesheet.css.erb file and get fingerprinting)
上级 d35c9122
...@@ -275,7 +275,7 @@ def favicon_link_tag(source='/favicon.ico', options={}) ...@@ -275,7 +275,7 @@ def favicon_link_tag(source='/favicon.ico', options={})
# plugin authors are encouraged to do so. # plugin authors are encouraged to do so.
def image_path(source) def image_path(source)
if config.use_sprockets if config.use_sprockets
sprockets_asset_path(source) asset_path(source)
else else
asset_paths.compute_public_path(source, 'images') asset_paths.compute_public_path(source, 'images')
end end
...@@ -294,7 +294,7 @@ def image_path(source) ...@@ -294,7 +294,7 @@ def image_path(source)
# video_path("http://www.railsapplication.com/vid/hd.avi") # => http://www.railsapplication.com/vid/hd.avi # video_path("http://www.railsapplication.com/vid/hd.avi") # => http://www.railsapplication.com/vid/hd.avi
def video_path(source) def video_path(source)
if config.use_sprockets if config.use_sprockets
sprockets_asset_path(source) asset_path(source)
else else
asset_paths.compute_public_path(source, 'videos') asset_paths.compute_public_path(source, 'videos')
end end
...@@ -313,7 +313,7 @@ def video_path(source) ...@@ -313,7 +313,7 @@ def video_path(source)
# audio_path("http://www.railsapplication.com/sounds/horse.wav") # => http://www.railsapplication.com/sounds/horse.wav # audio_path("http://www.railsapplication.com/sounds/horse.wav") # => http://www.railsapplication.com/sounds/horse.wav
def audio_path(source) def audio_path(source)
if config.use_sprockets if config.use_sprockets
sprockets_asset_path(source) asset_path(source)
else else
asset_paths.compute_public_path(source, 'audios') asset_paths.compute_public_path(source, 'audios')
end end
......
...@@ -87,7 +87,7 @@ def register_javascript_expansion(expansions) ...@@ -87,7 +87,7 @@ def register_javascript_expansion(expansions)
# javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js # javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js
def javascript_path(source) def javascript_path(source)
if config.use_sprockets if config.use_sprockets
sprockets_javascript_path(source) asset_path(source, 'js')
else else
asset_paths.compute_public_path(source, 'javascripts', 'js') asset_paths.compute_public_path(source, 'javascripts', 'js')
end end
......
...@@ -64,7 +64,7 @@ def register_stylesheet_expansion(expansions) ...@@ -64,7 +64,7 @@ def register_stylesheet_expansion(expansions)
# stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css # stylesheet_path "http://www.railsapplication.com/css/style.css" # => http://www.railsapplication.com/css/style.css
def stylesheet_path(source) def stylesheet_path(source)
if config.use_sprockets if config.use_sprockets
sprockets_stylesheet_path(source) asset_path(source, 'css')
else else
asset_paths.compute_public_path(source, 'stylesheets', 'css') asset_paths.compute_public_path(source, 'stylesheets', 'css')
end end
......
...@@ -3,23 +3,14 @@ ...@@ -3,23 +3,14 @@
module ActionView module ActionView
module Helpers module Helpers
module SprocketsHelper module SprocketsHelper
def sprockets_asset_path(source, default_ext = nil) def asset_path(source, default_ext = nil)
compute_sprockets_path(source, 'assets', default_ext) compute_sprockets_path(source, 'assets', default_ext)
end end
def sprockets_javascript_path(source)
sprockets_asset_path(source, 'js')
end
def sprockets_stylesheet_path(source)
sprockets_asset_path(source, 'css')
end
def sprockets_javascript_include_tag(source, options = {}) def sprockets_javascript_include_tag(source, options = {})
options = { options = {
'type' => "text/javascript", 'type' => "text/javascript",
'src' => sprockets_javascript_path(source) 'src' => asset_path(source, 'js')
}.merge(options.stringify_keys) }.merge(options.stringify_keys)
content_tag 'script', "", options content_tag 'script', "", options
...@@ -30,7 +21,7 @@ def sprockets_stylesheet_link_tag(source, options = {}) ...@@ -30,7 +21,7 @@ def sprockets_stylesheet_link_tag(source, options = {})
'rel' => "stylesheet", 'rel' => "stylesheet",
'type' => "text/css", 'type' => "text/css",
'media' => "screen", 'media' => "screen",
'href' => sprockets_stylesheet_path(source) 'href' => asset_path(source, 'css')
}.merge(options.stringify_keys) }.merge(options.stringify_keys)
tag 'link', options tag 'link', options
...@@ -60,10 +51,15 @@ def add_default_extension(source, default_ext) ...@@ -60,10 +51,15 @@ def add_default_extension(source, default_ext)
end end
def add_fingerprint(source, dir) def add_fingerprint(source, dir)
source.replace(assets.path($1, config.perform_caching, dir)) if source =~ /^\/#{dir}\/(.+)/ if source =~ /^\/#{dir}\/(.+)/
source.replace(assets.path($1, performing_caching?, dir))
end
end end
def add_asset_host(source) def add_asset_host(source)
# When included in Sprockets::Context, there's no controller
return unless respond_to?(:controller)
host = compute_asset_host(source) host = compute_asset_host(source)
if controller.respond_to?(:request) && host && URI.parse(host).host if controller.respond_to?(:request) && host && URI.parse(host).host
...@@ -94,6 +90,16 @@ def compute_asset_host(source) ...@@ -94,6 +90,16 @@ def compute_asset_host(source)
def assets def assets
Rails.application.assets Rails.application.assets
end end
def performing_caching?
# When included in Sprockets::Context, we need to ask the top-level config as the controller is not available
respond_to?(:config) ? config.perform_caching : Rails.application.config.action_controller.perform_caching
end
end end
end end
end end
# FIXME: Temp hack for extending Sprockets::Context so
class Sprockets::Context
include ActionView::Helpers::SprocketsHelper
end if defined?(Sprockets)
\ No newline at end of file
...@@ -33,38 +33,38 @@ def url_for(*args) ...@@ -33,38 +33,38 @@ def url_for(*args)
test "asset path" do test "asset path" do
assert_equal "/assets/logo-9c0a079bdd7701d7e729bd956823d153.png", assert_equal "/assets/logo-9c0a079bdd7701d7e729bd956823d153.png",
sprockets_asset_path("logo.png") asset_path("logo.png")
assert_equal "/images/logo", assert_equal "/images/logo",
sprockets_asset_path("/images/logo") asset_path("/images/logo")
assert_equal "/images/logo.gif", assert_equal "/images/logo.gif",
sprockets_asset_path("/images/logo.gif") asset_path("/images/logo.gif")
assert_equal "/dir/audio", assert_equal "/dir/audio",
sprockets_asset_path("/dir/audio") asset_path("/dir/audio")
assert_equal "http://www.example.com/video/play", assert_equal "http://www.example.com/video/play",
sprockets_asset_path("http://www.example.com/video/play") asset_path("http://www.example.com/video/play")
assert_equal "http://www.example.com/video/play.mp4", assert_equal "http://www.example.com/video/play.mp4",
sprockets_asset_path("http://www.example.com/video/play.mp4") asset_path("http://www.example.com/video/play.mp4")
end end
test "javascript path" do test "javascript path" do
assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.js", assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.js",
sprockets_javascript_path(:application) asset_path(:application, "js")
assert_equal "/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js", assert_equal "/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js",
sprockets_javascript_path("xmlhr") asset_path("xmlhr", "js")
assert_equal "/assets/dir/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js", assert_equal "/assets/dir/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js",
sprockets_javascript_path("dir/xmlhr.js") asset_path("dir/xmlhr.js", "js")
assert_equal "/dir/xmlhr.js", assert_equal "/dir/xmlhr.js",
sprockets_javascript_path("/dir/xmlhr") asset_path("/dir/xmlhr", "js")
assert_equal "http://www.railsapplication.com/js/xmlhr", assert_equal "http://www.railsapplication.com/js/xmlhr",
sprockets_javascript_path("http://www.railsapplication.com/js/xmlhr") asset_path("http://www.railsapplication.com/js/xmlhr", "js")
assert_equal "http://www.railsapplication.com/js/xmlhr.js", assert_equal "http://www.railsapplication.com/js/xmlhr.js",
sprockets_javascript_path("http://www.railsapplication.com/js/xmlhr.js") asset_path("http://www.railsapplication.com/js/xmlhr.js", "js")
end end
test "javascript include tag" do test "javascript include tag" do
...@@ -80,20 +80,16 @@ def url_for(*args) ...@@ -80,20 +80,16 @@ def url_for(*args)
end end
test "stylesheet path" do test "stylesheet path" do
assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.css", assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.css", asset_path(:application, "css")
sprockets_stylesheet_path(:application)
assert_equal "/assets/style-d41d8cd98f00b204e9800998ecf8427e.css", assert_equal "/assets/style-d41d8cd98f00b204e9800998ecf8427e.css", asset_path("style", "css")
sprockets_stylesheet_path("style") assert_equal "/assets/dir/style-d41d8cd98f00b204e9800998ecf8427e.css", asset_path("dir/style.css", "css")
assert_equal "/assets/dir/style-d41d8cd98f00b204e9800998ecf8427e.css", assert_equal "/dir/style.css", asset_path("/dir/style.css", "css")
sprockets_stylesheet_path("dir/style.css")
assert_equal "/dir/style.css",
sprockets_stylesheet_path("/dir/style.css")
assert_equal "http://www.railsapplication.com/css/style", assert_equal "http://www.railsapplication.com/css/style",
sprockets_stylesheet_path("http://www.railsapplication.com/css/style") asset_path("http://www.railsapplication.com/css/style", "css")
assert_equal "http://www.railsapplication.com/css/style.css", assert_equal "http://www.railsapplication.com/css/style.css",
sprockets_stylesheet_path("http://www.railsapplication.com/css/style.css") asset_path("http://www.railsapplication.com/css/style.css", "css")
end end
test "stylesheet link tag" do test "stylesheet link tag" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册