Add request protocol to asset host if not given. Prefer setting asset host as hostname only, no request protocol.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6162 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2007-02-18 07:23:57 +00:00
parent 90c1207c38
commit c9260c556c
2 changed files with 21 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ module ActionView
# in your environment.rb. These methods do not verify the assets exist before
# linking to them.
#
# ActionController::Base.asset_host = "http://assets.example.com"
# ActionController::Base.asset_host = "assets.example.com"
# image_tag("rails.png")
# => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
# stylesheet_include_tag("application")
@@ -212,7 +212,8 @@ module ActionView
# Add the .ext if not present. Return full URLs otherwise untouched.
# Prefix with /dir/ if lacking a leading /. Account for relative URL
# roots. Rewrite the asset path for cache-busting asset ids. Include
# a single or wildcarded asset host if configured.
# a single or wildcarded asset host, if configured, with the correct
# request protocol.
def compute_public_path(source, dir, ext)
source += ".#{ext}" if File.extname(source).blank?
if source =~ %r{^[-a-z]+://}
@@ -221,7 +222,13 @@ module ActionView
source = "/#{dir}/#{source}" unless source[0] == ?/
source = "#{@controller.request.relative_url_root}#{source}"
rewrite_asset_path!(source)
"#{compute_asset_host(source)}#{source}"
host = compute_asset_host(source)
unless host.blank? or host =~ %r{^[-a-z]+://}
host = "#{@controller.request.protocol}#{host}"
end
"#{host}#{source}"
end
end

View File

@@ -189,6 +189,10 @@ class AssetTagHelperNonVhostTest < Test::Unit::TestCase
def relative_url_root
"/collaboration/hieraki"
end
def protocol
'gopher://'
end
end.new
@controller.request = @request
@@ -230,4 +234,11 @@ class AssetTagHelperNonVhostTest < Test::Unit::TestCase
ensure
ActionController::Base.asset_host = nil
end
def test_asset_host_without_protocol_should_use_request_protocol
ActionController::Base.asset_host = 'a.example.com'
assert_equal 'gopher://a.example.com/collaboration/hieraki/images/xml.png', image_path('xml.png')
ensure
ActionController::Base.asset_host = nil
end
end