Skip to content

Commit

Permalink
[merb-assets] Fixed external asset urls when path_prefix is set
Browse files Browse the repository at this point in the history
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
merbjedi authored and snusnu committed Oct 21, 2009
1 parent 93fa05d commit 20b96c7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
10 changes: 7 additions & 3 deletions merb-assets/lib/merb-assets/assets.rb
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions merb-assets/lib/merb-assets/assets_mixin.rb
Expand Up @@ -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
Expand Down
83 changes: 82 additions & 1 deletion merb-assets/spec/merb-assets_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 20b96c7

Please sign in to comment.