Skip to content

Commit

Permalink
Compute asset paths from the request if ENV["RAILS_RELATIVE_URL_ROOT"…
Browse files Browse the repository at this point in the history
…] is not set.

This should fix the following issue:

"Rails is not a Rack Application (SCRIPT_NAME vs. relative_url_root ?)"
rails#910
  • Loading branch information
dlitz committed Mar 6, 2012
1 parent dc80715 commit 8e26d29
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/asset_paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def compute_asset_host(source)
end

def relative_url_root
config.relative_url_root
config.relative_url_root || current_request.try(:script_name)
end

def asset_host_config
Expand Down
102 changes: 102 additions & 0 deletions actionpack/test/template/asset_tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def setup
@controller = BasicController.new

@request = Class.new do
attr_accessor :script_name
def protocol() 'http://' end
def ssl?() false end
def host_with_port() 'localhost' end
Expand Down Expand Up @@ -647,6 +648,13 @@ def test_timebased_asset_id_with_relative_url_root
assert_equal %(<img alt="Rails" src="#{@controller.config.relative_url_root}/images/rails.png?#{expected_time}" />), image_tag("rails.png")
end

# Same as above, but with script_name
def test_timebased_asset_id_with_script_name
@request.script_name = "/collaboration/hieraki"
expected_time = File.mtime(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).to_i.to_s
assert_equal %(<img alt="Rails" src="#{@request.script_name}/images/rails.png?#{expected_time}" />), image_tag("rails.png")
end

def test_should_skip_asset_id_on_complete_url
assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png")
end
Expand Down Expand Up @@ -897,6 +905,31 @@ def test_caching_javascript_include_tag_with_relative_url_root
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
end

# Same as above, but with script_name
def test_caching_javascript_include_tag_with_script_name
ENV["RAILS_ASSET_ID"] = ""
@request.script_name = "/collaboration/hieraki"
config.perform_caching = true

assert_dom_equal(
%(<script src="/collaboration/hieraki/javascripts/all.js" type="text/javascript"></script>),
javascript_include_tag(:all, :cache => true)
)

assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))

assert_dom_equal(
%(<script src="/collaboration/hieraki/javascripts/money.js" type="text/javascript"></script>),
javascript_include_tag(:all, :cache => "money")
)

assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))

ensure
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
end

def test_caching_javascript_include_tag_with_named_paths_and_relative_url_root_when_caching_off
ENV["RAILS_ASSET_ID"] = ""
@controller.config.relative_url_root = "/collaboration/hieraki"
Expand All @@ -917,6 +950,27 @@ def test_caching_javascript_include_tag_with_named_paths_and_relative_url_root_w
assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
end

# Same as above, but with script_name
def test_caching_javascript_include_tag_with_named_paths_and_script_name_when_caching_off
ENV["RAILS_ASSET_ID"] = ""
@request.script_name = "/collaboration/hieraki"
config.perform_caching = false

assert_dom_equal(
%(<script src="/collaboration/hieraki/javascripts/robber.js" type="text/javascript"></script>),
javascript_include_tag('robber', :cache => true)
)

assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))

assert_dom_equal(
%(<script src="/collaboration/hieraki/javascripts/robber.js" type="text/javascript"></script>),
javascript_include_tag('robber', :cache => "money", :recursive => true)
)

assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
end

def test_caching_javascript_include_tag_when_caching_off
ENV["RAILS_ASSET_ID"] = ""
config.perform_caching = false
Expand Down Expand Up @@ -1144,6 +1198,33 @@ def test_caching_stylesheet_link_tag_with_relative_url_root
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
end

# Same as above, but with script_name
def test_caching_stylesheet_link_tag_with_script_name
ENV["RAILS_ASSET_ID"] = ""
@request.script_name = "/collaboration/hieraki"
config.perform_caching = true

assert_dom_equal(
%(<link href="/collaboration/hieraki/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />),
stylesheet_link_tag(:all, :cache => true)
)

files_to_be_joined = Dir["#{ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR}/[^all]*.css"]

expected_mtime = files_to_be_joined.map { |p| File.mtime(p) }.max
assert_equal expected_mtime, File.mtime(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))

assert_dom_equal(
%(<link href="/collaboration/hieraki/stylesheets/money.css" media="screen" rel="stylesheet" type="text/css" />),
stylesheet_link_tag(:all, :cache => "money")
)

assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
ensure
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
end


def test_caching_stylesheet_link_tag_with_named_paths_and_relative_url_root_when_caching_off
ENV["RAILS_ASSET_ID"] = ""
Expand All @@ -1165,6 +1246,27 @@ def test_caching_stylesheet_link_tag_with_named_paths_and_relative_url_root_when
assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
end

# Same as above, but with script_name
def test_caching_stylesheet_link_tag_with_named_paths_and_script_name_when_caching_off
ENV["RAILS_ASSET_ID"] = ""
@request.script_name = "/collaboration/hieraki"
config.perform_caching = false

assert_dom_equal(
%(<link href="/collaboration/hieraki/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />),
stylesheet_link_tag('robber', :cache => true)
)

assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))

assert_dom_equal(
%(<link href="/collaboration/hieraki/stylesheets/robber.css" media="screen" rel="stylesheet" type="text/css" />),
stylesheet_link_tag('robber', :cache => "money")
)

assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
end




Expand Down
1 change: 1 addition & 0 deletions actionpack/test/template/sprockets_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MockRequest
def protocol() 'http://' end
def ssl?() false end
def host_with_port() 'localhost' end
def script_name() nil end
end

def setup
Expand Down

0 comments on commit 8e26d29

Please sign in to comment.