Skip to content
Browse files

Allow asset url config to be undefined

  • Loading branch information...
1 parent 5dfeb1b commit 60a4fffd83e94ad4471570b16f9d954f04bc0300 @josh josh committed Oct 13, 2012
View
6 actionpack/lib/action_view/helpers/asset_url_helper.rb
@@ -135,7 +135,9 @@ def asset_path(source, options = {})
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 @@ def compute_asset_path(source, options = {})
# (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
View
41 actionpack/test/template/asset_tag_helper_test.rb
@@ -733,3 +733,44 @@ def test_asset_url
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

0 comments on commit 60a4fff

Please sign in to comment.
Something went wrong with that request. Please try again.