From 60a4fffd83e94ad4471570b16f9d954f04bc0300 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 13 Oct 2012 10:13:47 -0500 Subject: [PATCH] Allow asset url config to be undefined --- .../action_view/helpers/asset_url_helper.rb | 6 ++- .../test/template/asset_tag_helper_test.rb | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/asset_url_helper.rb b/actionpack/lib/action_view/helpers/asset_url_helper.rb index 9255c1e2f6..a0fcac59a3 100644 --- a/actionpack/lib/action_view/helpers/asset_url_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_url_helper.rb @@ -135,7 +135,9 @@ module ActionView source = compute_asset_path(source, options) end - if relative_url_root = config.relative_url_root || asset_request.try(:script_name) + relative_url_root = (defined?(config.relative_url_root) && config.relative_url_root) || + (asset_request && asset_request.script_name) + if relative_url_root source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/") end @@ -180,7 +182,7 @@ module ActionView # (proc or otherwise). def compute_asset_host(source = "", options = {}) request = asset_request - host = config.asset_host + host = config.asset_host if defined? config.asset_host host ||= request.base_url if request && options[:protocol] == :request return unless host diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 5dc854d561..8435db3166 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -733,3 +733,44 @@ class AssetUrlHelperControllerTest < ActionView::TestCase assert_equal "http://www.example.com/foo", @controller.asset_url("foo") end end + +class AssetUrlHelperEmptyModuleTest < ActionView::TestCase + tests ActionView::Helpers::AssetUrlHelper + + def setup + super + + @module = Module.new + @module.extend ActionView::Helpers::AssetUrlHelper + end + + def test_asset_path + assert_equal "/foo", @module.asset_path("foo") + end + + def test_asset_url + assert_equal "/foo", @module.asset_url("foo") + end + + def test_asset_url_with_request + @module.instance_eval do + def request + Struct.new(:base_url, :script_name).new("http://www.example.com", nil) + end + end + + assert @module.request + assert_equal "http://www.example.com/foo", @module.asset_url("foo") + end + + def test_asset_url_with_config_asset_host + @module.instance_eval do + def config + Struct.new(:asset_host).new("http://www.example.com") + end + end + + assert @module.config.asset_host + assert_equal "http://www.example.com/foo", @module.asset_url("foo") + end +end