Permalink
Browse files

Merge pull request #23 from rails/new-asset-url-api

Use new asset url helper apis
  • Loading branch information...
2 parents e7146b7 + 987f9a4 commit 13234e3c40865d14fa056a05d7e7f3fd545a858c @rafaelfranca rafaelfranca committed Oct 15, 2012
@@ -2,10 +2,6 @@ module Sprockets
module Rails
module Helpers
module IsolatedHelper
- def controller
- nil
- end
-
def config
::Rails.application.config.action_controller
end
@@ -7,16 +7,7 @@ module RailsHelper
extend ActiveSupport::Concern
include ActionView::Helpers::AssetTagHelper
- def asset_paths
- @asset_paths ||= begin
- paths = RailsHelper::AssetPaths.new(config, controller)
- paths.asset_environment = asset_environment
- paths.asset_digests = asset_digests
- paths.compile_assets = compile_assets?
- paths.digest_assets = digest_assets?
- paths
- end
- end
+ class AssetNotPrecompiledError < StandardError; end
def javascript_include_tag(*sources)
options = sources.extract_options!
@@ -25,12 +16,12 @@ def javascript_include_tag(*sources)
digest = options.delete(:digest) { digest_assets? }
sources.collect do |source|
- if debug && asset = asset_paths.asset_for(source, 'js')
+ if debug && asset = asset_for(source, :type => :javascript)
asset.to_a.map { |dep|
- super(dep.pathname.to_s, { :src => path_to_asset(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
+ super(dep.pathname.to_s, { :src => path_to_javascript(dep.logical_path, :body => true, :digest => digest) }.merge!(options))
}
else
- super(source.to_s, { :src => path_to_asset(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
+ super(source.to_s, { :src => path_to_javascript(source, :body => body, :digest => digest) }.merge!(options))
end
end.uniq.join("\n").html_safe
end
@@ -42,44 +33,53 @@ def stylesheet_link_tag(*sources)
digest = options.delete(:digest) { digest_assets? }
sources.collect do |source|
- if debug && asset = asset_paths.asset_for(source, 'css')
+ if debug && asset = asset_for(source, :type => :stylesheet)
asset.to_a.map { |dep|
- super(dep.pathname.to_s, { :href => path_to_asset(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
+ super(dep.pathname.to_s, { :href => path_to_stylesheet(dep.logical_path, :body => true, :protocol => :request, :digest => digest) }.merge!(options))
}
else
- super(source.to_s, { :href => path_to_asset(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
+ super(source.to_s, { :href => path_to_stylesheet(source, :body => body, :protocol => :request, :digest => digest) }.merge!(options))
end
end.uniq.join("\n").html_safe
end
- def asset_path(source, options = {})
- source = source.logical_path if source.respond_to?(:logical_path)
- path = asset_paths.compute_public_path(source, asset_prefix, options.merge(:body => true))
- options[:body] ? "#{path}?body=1" : path
- end
- alias_method :path_to_asset, :asset_path # aliased to avoid conflicts with an asset_path named route
-
- def image_path(source)
- path_to_asset(source)
+ def compute_asset_path(source, options = {})
+ if digest_assets? && options[:digest] != false
+ source = digest_for(source)
+ end
+ source = File.join(asset_prefix, source)
+ source = "/#{source}" unless source =~ /^\//
+ options[:body] ? "#{source}?body=1" : source
end
- alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
- def font_path(source)
- path_to_asset(source)
+ private
+ # Retrieve the asset path on disk, for processed files +ext+ should
+ # contain the final extension (e.g. +js+ for <tt>*.js.coffee</tt>).
+ def asset_for(source, options)
+ source = source.to_s
+ if extname = compute_asset_extname(source, options)
+ source = "#{source}#{extname}"
+ end
+ asset_environment[source]
+ rescue Sprockets::FileOutsidePaths
+ nil
end
- alias_method :path_to_font, :font_path # aliased to avoid conflicts with a font_path named route
- def javascript_path(source)
- path_to_asset(source, :ext => 'js')
- end
- alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
+ def digest_for(logical_path)
+ if digest_assets? && asset_digests && (digest = asset_digests[logical_path])
+ return digest
+ end
- def stylesheet_path(source)
- path_to_asset(source, :ext => 'css')
+ if compile_assets?
+ if digest_assets? && asset = asset_environment[logical_path]
+ return asset.digest_path
+ end
+ return logical_path
+ else
+ raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
+ end
end
- alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
- private
def debug_assets?
compile_assets? && (::Rails.application.config.assets.debug || params[:debug_assets])
rescue NameError
@@ -114,65 +114,6 @@ def digest_assets?
def asset_environment
::Rails.application.assets
end
-
- class AssetPaths < ::ActionView::AssetPaths #:nodoc:
- attr_accessor :asset_environment, :asset_prefix, :asset_digests, :compile_assets, :digest_assets
-
- class AssetNotPrecompiledError < StandardError; end
-
- # Retrieve the asset path on disk, for processed files +ext+ should
- # contain the final extension (e.g. +js+ for <tt>*.js.coffee</tt>).
- def asset_for(source, ext)
- source = source.to_s
- return nil if is_uri?(source)
- source = rewrite_extension(source, nil, ext)
- asset_environment[source]
- rescue Sprockets::FileOutsidePaths
- nil
- end
-
- def digest_for(logical_path)
- if digest_assets && asset_digests && (digest = asset_digests[logical_path])
- return digest
- end
-
- if compile_assets
- if digest_assets && asset = asset_environment[logical_path]
- return asset.digest_path
- end
- return logical_path
- else
- raise AssetNotPrecompiledError.new("#{logical_path} isn't precompiled")
- end
- end
-
- def rewrite_asset_path(source, dir, options = {})
- if source[0] == ?/
- source
- else
- if digest_assets && options[:digest] != false
- source = digest_for(source)
- end
- source = File.join(dir, source)
- source = "/#{source}" unless source =~ /^\//
- source
- end
- end
-
- def rewrite_extension(source, dir, ext)
- source_ext = File.extname(source)
- if ext && source_ext != ".#{ext}"
- if !source_ext.empty? && (asset = asset_environment[source]) &&
- asset.pathname.to_s =~ /#{source}\Z/
- source
- else
- "#{source}.#{ext}"
- end
- else
- source
- end
- end
- end
end
end
end
@@ -3,18 +3,19 @@
class SprocketsHelperTest < ActiveSupport::TestCase
include Sprockets::Rails::Helpers::RailsHelper
- attr_accessor :assets, :controller, :params
+ attr_accessor :assets, :controller, :params, :request
class MockRequest
def protocol() 'http://' end
def ssl?() false end
def host_with_port() 'localhost' end
def script_name() nil end
+ def base_url() 'http://www.example.com' end
end
def setup
@controller = BasicController.new
- @controller.request = MockRequest.new
+ @controller.request = @request = MockRequest.new
@assets = Sprockets::Environment.new
@assets.append_path(FIXTURES.join("app/javascripts"))
@@ -119,20 +120,6 @@ def compute_host(source, request, options = {})
asset_path("logo.png")
end
- test "stylesheets served without a controller in scope cannot access the request" do
- @controller = nil
- @config.asset_host = Proc.new do |asset, request|
- fail "This should not have been called."
- end
- assert_raises ActionView::MissingRequestError do
- asset_path("logo.png")
- end
- @config.asset_host = method :compute_host
- assert_raises ActionView::MissingRequestError do
- asset_path("logo.png")
- end
- end
-
test "image_tag" do
assert_dom_equal '<img alt="Xml" src="/assets/xml.png" />', image_tag("xml.png")
end
@@ -176,7 +163,6 @@ def compute_host(source, request, options = {})
end
test "stylesheets served without a controller in do not use asset hosts when the default protocol is :request" do
- @controller = nil
@config.asset_host = "assets-%d.example.com"
@config.default_asset_host_protocol = :request
@config.perform_caching = true
@@ -192,7 +178,6 @@ def compute_host(source, request, options = {})
end
test "asset path with relative url root when controller isn't present but relative_url_root is" do
- @controller = nil
@config.relative_url_root = "/collaboration/hieraki"
assert_equal "/collaboration/hieraki/images/logo.gif",
asset_path("/images/logo.gif")
@@ -211,20 +196,20 @@ def compute_host(source, request, options = {})
test "javascript path through asset_path" do
assert_match %r{/assets/application-[0-9a-f]+.js},
- asset_path(:application, :ext => "js")
+ asset_path(:application, :type => :javascript)
assert_match %r{/assets/xmlhr-[0-9a-f]+.js},
- asset_path("xmlhr", :ext => "js")
+ asset_path("xmlhr", :type => :javascript)
assert_match %r{/assets/dir/xmlhr-[0-9a-f]+.js},
- asset_path("dir/xmlhr.js", :ext => "js")
+ asset_path("dir/xmlhr.js", :type => :javascript)
assert_equal "/dir/xmlhr.js",
- asset_path("/dir/xmlhr", :ext => "js")
+ asset_path("/dir/xmlhr", :type => :javascript)
assert_equal "http://www.example.com/js/xmlhr",
- asset_path("http://www.example.com/js/xmlhr", :ext => "js")
+ asset_path("http://www.example.com/js/xmlhr", :type => :javascript)
assert_equal "http://www.example.com/js/xmlhr.js",
- asset_path("http://www.example.com/js/xmlhr.js", :ext => "js")
+ asset_path("http://www.example.com/js/xmlhr.js", :type => :javascript)
end
test "javascript include tag" do
@@ -266,19 +251,21 @@ def compute_host(source, request, options = {})
end
test "stylesheet path through asset_path" do
- assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :ext => "css")
+ assert_match %r{/assets/application-[0-9a-f]+.css}, asset_path(:application, :type => :stylesheet)
- 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_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :type => :stylesheet)
+ assert_match %r{/assets/dir/style-[0-9a-f]+.css}, asset_path("dir/style.css", :type => :stylesheet)
+ assert_equal "/dir/style.css", asset_path("/dir/style.css", :type => :stylesheet)
assert_equal "http://www.example.com/css/style",
- asset_path("http://www.example.com/css/style", :ext => "css")
+ asset_path("http://www.example.com/css/style", :type => :stylesheet)
assert_equal "http://www.example.com/css/style.css",
- asset_path("http://www.example.com/css/style.css", :ext => "css")
+ asset_path("http://www.example.com/css/style.css", :type => :stylesheet)
end
test "stylesheet link tag" do
+ @request = nil
+
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
stylesheet_link_tag(:application)
assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" />},
@@ -307,9 +294,6 @@ def compute_host(source, request, options = {})
assert_match %r{\A<link href="/assets/style-[0-9a-f]+.css" media="screen" rel="stylesheet" />\Z},
stylesheet_link_tag("style", "style")
- assert_match %r{\A<link href="/assets/style-[0-9a-f]+.ext" media="screen" rel="stylesheet" />\Z},
- stylesheet_link_tag("style.ext")
-
assert_match %r{\A<link href="/assets/style.min-[0-9a-f]+.css" media="screen" rel="stylesheet" />\Z},
stylesheet_link_tag("style.min")
@@ -327,25 +311,25 @@ def compute_host(source, request, options = {})
test "alternate asset prefix" do
stubs(:asset_prefix).returns("/themes/test")
- assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
+ assert_match %r{/themes/test/style-[0-9a-f]+.css}, asset_path("style", :type => :stylesheet)
end
test "alternate asset environment" do
assets = Sprockets::Environment.new
assets.append_path(FIXTURES.join("alternate/stylesheets"))
stubs(:asset_environment).returns(assets)
- assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :ext => "css")
+ assert_match %r{/assets/style-[0-9a-f]+.css}, asset_path("style", :type => :stylesheet)
end
test "alternate hash based on environment" do
assets = Sprockets::Environment.new
assets.version = 'development'
assets.append_path(FIXTURES.join("alternate/stylesheets"))
stubs(:asset_environment).returns(assets)
- dev_path = asset_path("style", :ext => "css")
+ dev_path = asset_path("style", :type => :stylesheet)
assets.version = 'production'
- prod_path = asset_path("style", :ext => "css")
+ prod_path = asset_path("style", :type => :stylesheet)
assert_not_equal prod_path, dev_path
end

0 comments on commit 13234e3

Please sign in to comment.