Permalink
Browse files

[merb-assets] Fixed external asset urls when path_prefix is set

Added checks for relative, absolute, and external
urls for when path_prefix is set in Merb::Config.
path_prefix should be ignored when an external url
is requested, however it should be used when absolute
or relative urls are passed.

[#614 state:resolved]
  • Loading branch information...
1 parent 93fa05d commit 20b96c7f455b54429052e1ba7a5e2e1139adfe6e @merbjedi merbjedi committed with snusnu Oct 16, 2009
@@ -41,12 +41,16 @@ module AssetHelpers
# # => "public/javascripts/dingo.js"
def asset_path(asset_type, filename, local_path = false)
filename = filename.to_s
+ return filename if filename =~ %r{^https?://} #leave absolte paths alone
+
+ # add extension if none given
if filename !~ /#{'\\' + ASSET_FILE_EXTENSIONS[asset_type]}\Z/ && filename.index('?').nil?
filename = "#{filename}#{ASSET_FILE_EXTENSIONS[asset_type]}" # don't modify receiver
end
- if filename !~ %r{^(/|https?://)}
- filename = "/#{asset_type}s/#{filename}"
- end
+
+ # prepend asset type's folder path
+ filename = "/#{asset_type}s/#{filename}" unless filename.start_with?("/")
+
if local_path
return "public#{filename}"
else
@@ -165,18 +165,14 @@ def link_to(name, url='', opts={})
def image_tag(img, opts={})
return "" if img.blank?
if img[0].chr == '/'
- opts[:src] = img
+ opts[:src] = "#{Merb::Config[:path_prefix]}#{img}"
else
opts[:path] ||=
if img =~ %r{^https?://}
absolute = true
''
else
- if Merb::Config[:path_prefix]
- Merb::Config[:path_prefix] + '/images/'
- else
- '/images/'
- end
+ "#{Merb::Config[:path_prefix]}/images/"
end
opts[:src] ||= opts.delete(:path) + img
end
@@ -68,7 +68,88 @@
end
end
-describe "With Merb::Config[:reload_templates] set" do
+describe "With Merb::Config[:path_prefix] set," do
+ before(:all) do
+ Merb::Config[:path_prefix] = "/myapp"
+ end
+ after(:all) do
+ Merb::Config[:path_prefix] = nil
+ end
+
+ describe "create an image tag" do
+ it "with a relative url" do
+ image_tag('foo.gif').should ==
+ "<img src=\"/myapp/images/foo.gif\" />"
+ end
+
+ it "with an absolute url" do
+ image_tag('/foo.gif').should ==
+ "<img src=\"/myapp/foo.gif\" />"
+ end
+
+ it "with an external url" do
+ image_tag('http://example.com/foo.gif').should ==
+ "<img src=\"http://example.com/foo.gif\" />"
+ end
+ end
+
+ describe "create a stylesheet include tag" do
+ it "with a relative url" do
+ result = css_include_tag('bar.css')
+ result.should match(%r{^<link})
+ result.should match(%r{media="all"})
+ result.should match(%r{type="text/css"})
+ result.should match(%r{href="/myapp/stylesheets/bar.css"})
+ result.should match(%r{charset="utf-8"})
+ result.should match(%r{rel="Stylesheet"})
+ end
+
+ it "with an absolute url" do
+ result = css_include_tag('/bar.css')
+ result.should match(%r{^<link})
+ result.should match(%r{media="all"})
+ result.should match(%r{type="text/css"})
+ result.should match(%r{href="/myapp/bar.css"})
+ result.should match(%r{charset="utf-8"})
+ result.should match(%r{rel="Stylesheet"})
+ end
+
+ it "with an external url" do
+ result = css_include_tag('http://example.com/bar.css')
+ result.should match(%r{^<link})
+ result.should match(%r{media="all"})
+ result.should match(%r{type="text/css"})
+ result.should match(%r{href="http://example.com/bar.css"})
+ result.should match(%r{charset="utf-8"})
+ result.should match(%r{rel="Stylesheet"})
+ end
+ end
+
+ describe "create a javascript include tag" do
+ it "with a relative url" do
+ result = js_include_tag("bar.js")
+ result.should match(%r{^<script}); result.should match(%r{</script>$})
+ result.should match(%r{type="text/javascript"})
+ result.should match(%r{src="/myapp/javascripts/bar.js"})
+ end
+
+ it "with an absolute url" do
+ result = js_include_tag("/bar.js")
+ result.should match(%r{^<script}); result.should match(%r{</script>$})
+ result.should match(%r{type="text/javascript"})
+ result.should match(%r{src="/myapp/bar.js"})
+ end
+
+ it "with an external url" do
+ result = js_include_tag("http://example.com/bar.js")
+ result.should match(%r{^<script}); result.should match(%r{</script>$})
+ result.should match(%r{type="text/javascript"})
+ result.should match(%r{src="http://example.com/bar.js"})
+ end
+ end
+end
+
+describe "With Merb::Config[:reload_templates] set," do
before(:all) do
Merb::Config[:reload_templates] = true
end

0 comments on commit 20b96c7

Please sign in to comment.