aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdrian Pike <adrian@pikeapps.com>2011-10-24 15:57:14 -0700
committerAdrian Pike <adrian@pikeapps.com>2011-12-05 17:17:39 -0800
commit25007ad3fb3eac28a5f7ad3f6cd26eb33e14b4e0 (patch)
tree65c3499e363d5e590b0f7fdcd1ea4d13aede767d
parent8aa7b8695dd90989de699e050b61d755e3a61b19 (diff)
downloadrails-25007ad3fb3eac28a5f7ad3f6cd26eb33e14b4e0.tar.gz
rails-25007ad3fb3eac28a5f7ad3f6cd26eb33e14b4e0.tar.bz2
rails-25007ad3fb3eac28a5f7ad3f6cd26eb33e14b4e0.zip
Let's do the same trick for asset_path that we do for [image,javascript,stylesheet]_path to avoid namespace conflicts on named routes.
Closes #3427
-rw-r--r--actionpack/lib/sprockets/helpers/rails_helper.rb17
-rw-r--r--actionpack/test/template/sprockets_helper_with_routes_test.rb57
2 files changed, 66 insertions, 8 deletions
diff --git a/actionpack/lib/sprockets/helpers/rails_helper.rb b/actionpack/lib/sprockets/helpers/rails_helper.rb
index 1ebe7f68f7..75ba1e66c2 100644
--- a/actionpack/lib/sprockets/helpers/rails_helper.rb
+++ b/actionpack/lib/sprockets/helpers/rails_helper.rb
@@ -26,10 +26,10 @@ module Sprockets
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, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
+ super(dep.to_s, { :src => path_to_asset(dep, :ext => 'js', :body => true, :digest => digest) }.merge!(options))
}
else
- super(source.to_s, { :src => asset_path(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
+ super(source.to_s, { :src => path_to_asset(source, :ext => 'js', :body => body, :digest => digest) }.merge!(options))
end
end.join("\n").html_safe
end
@@ -43,10 +43,10 @@ module Sprockets
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, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
+ super(dep.to_s, { :href => path_to_asset(dep, :ext => 'css', :body => true, :protocol => :request, :digest => digest) }.merge!(options))
}
else
- super(source.to_s, { :href => asset_path(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
+ super(source.to_s, { :href => path_to_asset(source, :ext => 'css', :body => body, :protocol => :request, :digest => digest) }.merge!(options))
end
end.join("\n").html_safe
end
@@ -56,19 +56,20 @@ module Sprockets
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)
- asset_path(source)
+ path_to_asset(source)
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
def javascript_path(source)
- asset_path(source)
+ path_to_asset(source)
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with an javascript_path named route
def stylesheet_path(source)
- asset_path(source)
+ path_to_asset(source)
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with an stylesheet_path named route
diff --git a/actionpack/test/template/sprockets_helper_with_routes_test.rb b/actionpack/test/template/sprockets_helper_with_routes_test.rb
new file mode 100644
index 0000000000..bcbd81a7dd
--- /dev/null
+++ b/actionpack/test/template/sprockets_helper_with_routes_test.rb
@@ -0,0 +1,57 @@
+require 'abstract_unit'
+require 'sprockets'
+require 'sprockets/helpers/rails_helper'
+require 'mocha'
+
+class SprocketsHelperWithRoutesTest < ActionView::TestCase
+ include Sprockets::Helpers::RailsHelper
+
+ # Let's bring in some named routes to test namespace conflicts with potential *_paths.
+ # We have to do this after we bring in the Sprockets RailsHelper so if there are conflicts,
+ # they'll fail in the way we expect in a real live Rails app.
+ routes = ActionDispatch::Routing::RouteSet.new
+ routes.draw do
+ resources :assets
+ end
+ include routes.url_helpers
+
+ def setup
+ super
+ @controller = BasicController.new
+
+ @assets = Sprockets::Environment.new
+ @assets.append_path(FIXTURES.join("sprockets/app/javascripts"))
+ @assets.append_path(FIXTURES.join("sprockets/app/stylesheets"))
+ @assets.append_path(FIXTURES.join("sprockets/app/images"))
+
+ application = Struct.new(:config, :assets).new(config, @assets)
+ Rails.stubs(:application).returns(application)
+ @config = config
+ @config.perform_caching = true
+ @config.assets.digest = true
+ @config.assets.compile = true
+ end
+
+ test "namespace conflicts on a named route called asset_path" do
+ # Testing this for sanity - asset_path is now a named route!
+ assert_match asset_path('test_asset'), '/assets/test_asset'
+
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
+ path_to_asset("logo.png")
+ assert_match %r{/assets/logo-[0-9a-f]+.png},
+ path_to_asset("logo.png", :digest => true)
+ assert_match %r{/assets/logo.png},
+ path_to_asset("logo.png", :digest => false)
+ end
+
+ test "javascript_include_tag with a named_route named asset_path" do
+ assert_match %r{<script src="/assets/application-[0-9a-f]+.js" type="text/javascript"></script>},
+ javascript_include_tag(:application)
+ end
+
+ test "stylesheet_link_tag with a named_route named asset_path" do
+ assert_match %r{<link href="/assets/application-[0-9a-f]+.css" media="screen" rel="stylesheet" type="text/css" />},
+ stylesheet_link_tag(:application)
+ end
+
+end \ No newline at end of file