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={})
# plugin authors are encouraged to do so.
def image_path(source)
if config.use_sprockets
sprockets_asset_path(source)
asset_path(source)
else
asset_paths.compute_public_path(source, 'images')
end
......@@ -294,7 +294,7 @@ def image_path(source)
# video_path("http://www.railsapplication.com/vid/hd.avi") # => http://www.railsapplication.com/vid/hd.avi
def video_path(source)
if config.use_sprockets
sprockets_asset_path(source)
asset_path(source)
else
asset_paths.compute_public_path(source, 'videos')
end
......@@ -313,7 +313,7 @@ def video_path(source)
# audio_path("http://www.railsapplication.com/sounds/horse.wav") # => http://www.railsapplication.com/sounds/horse.wav
def audio_path(source)
if config.use_sprockets
sprockets_asset_path(source)
asset_path(source)
else
asset_paths.compute_public_path(source, 'audios')
end
......
......@@ -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
def javascript_path(source)
if config.use_sprockets
sprockets_javascript_path(source)
asset_path(source, 'js')
else
asset_paths.compute_public_path(source, 'javascripts', 'js')
end
......
......@@ -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
def stylesheet_path(source)
if config.use_sprockets
sprockets_stylesheet_path(source)
asset_path(source, 'css')
else
asset_paths.compute_public_path(source, 'stylesheets', 'css')
end
......
......@@ -3,23 +3,14 @@
module ActionView
module Helpers
module SprocketsHelper
def sprockets_asset_path(source, default_ext = nil)
def asset_path(source, default_ext = nil)
compute_sprockets_path(source, 'assets', default_ext)
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 = {})
options = {
'type' => "text/javascript",
'src' => sprockets_javascript_path(source)
'src' => asset_path(source, 'js')
}.merge(options.stringify_keys)
content_tag 'script', "", options
......@@ -30,7 +21,7 @@ def sprockets_stylesheet_link_tag(source, options = {})
'rel' => "stylesheet",
'type' => "text/css",
'media' => "screen",
'href' => sprockets_stylesheet_path(source)
'href' => asset_path(source, 'css')
}.merge(options.stringify_keys)
tag 'link', options
......@@ -60,10 +51,15 @@ def add_default_extension(source, default_ext)
end
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
def add_asset_host(source)
# When included in Sprockets::Context, there's no controller
return unless respond_to?(:controller)
host = compute_asset_host(source)
if controller.respond_to?(:request) && host && URI.parse(host).host
......@@ -94,6 +90,16 @@ def compute_asset_host(source)
def assets
Rails.application.assets
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
# 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)
test "asset path" do
assert_equal "/assets/logo-9c0a079bdd7701d7e729bd956823d153.png",
sprockets_asset_path("logo.png")
asset_path("logo.png")
assert_equal "/images/logo",
sprockets_asset_path("/images/logo")
asset_path("/images/logo")
assert_equal "/images/logo.gif",
sprockets_asset_path("/images/logo.gif")
asset_path("/images/logo.gif")
assert_equal "/dir/audio",
sprockets_asset_path("/dir/audio")
asset_path("/dir/audio")
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",
sprockets_asset_path("http://www.example.com/video/play.mp4")
asset_path("http://www.example.com/video/play.mp4")
end
test "javascript path" do
assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.js",
sprockets_javascript_path(:application)
asset_path(:application, "js")
assert_equal "/assets/xmlhr-d41d8cd98f00b204e9800998ecf8427e.js",
sprockets_javascript_path("xmlhr")
asset_path("xmlhr", "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",
sprockets_javascript_path("/dir/xmlhr")
asset_path("/dir/xmlhr", "js")
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",
sprockets_javascript_path("http://www.railsapplication.com/js/xmlhr.js")
asset_path("http://www.railsapplication.com/js/xmlhr.js", "js")
end
test "javascript include tag" do
......@@ -80,20 +80,16 @@ def url_for(*args)
end
test "stylesheet path" do
assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.css",
sprockets_stylesheet_path(:application)
assert_equal "/assets/application-d41d8cd98f00b204e9800998ecf8427e.css", asset_path(:application, "css")
assert_equal "/assets/style-d41d8cd98f00b204e9800998ecf8427e.css",
sprockets_stylesheet_path("style")
assert_equal "/assets/dir/style-d41d8cd98f00b204e9800998ecf8427e.css",
sprockets_stylesheet_path("dir/style.css")
assert_equal "/dir/style.css",
sprockets_stylesheet_path("/dir/style.css")
assert_equal "/assets/style-d41d8cd98f00b204e9800998ecf8427e.css", asset_path("style", "css")
assert_equal "/assets/dir/style-d41d8cd98f00b204e9800998ecf8427e.css", asset_path("dir/style.css", "css")
assert_equal "/dir/style.css", asset_path("/dir/style.css", "css")
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",
sprockets_stylesheet_path("http://www.railsapplication.com/css/style.css")
asset_path("http://www.railsapplication.com/css/style.css", "css")
end
test "stylesheet link tag" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册