Skip to content

Commit

Permalink
Ensure AssetUrlHelper can be mixed into AC::Base
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Oct 12, 2012
1 parent 1e2b0ce commit c3cff4d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
19 changes: 13 additions & 6 deletions actionpack/lib/action_view/helpers/asset_url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def asset_path(source, options = {})
source = compute_asset_path(source, options)
end

current_request = controller.request if controller.respond_to?(:request)
if relative_url_root = config.relative_url_root || current_request.try(:script_name)
if relative_url_root = config.relative_url_root || asset_request.try(:script_name)
source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/")
end

Expand Down Expand Up @@ -180,10 +179,7 @@ def compute_asset_path(source, options = {})
# or the value returned from invoking call on an object responding to call
# (proc or otherwise).
def compute_asset_host(source = "", options = {})
if controller.respond_to?(:request)
request = controller.request
end

request = asset_request
host = config.asset_host
host ||= request.base_url if request && options[:protocol] == :request
return unless host
Expand Down Expand Up @@ -342,6 +338,17 @@ def font_url(source)
url_to_asset(source, type: :font)
end
alias_method :url_to_font, :font_url # aliased to avoid conflicts with an font_url named route

private
# Get current request if self is a controller. If self is a
# view, check the parent controller's request.
def asset_request
if respond_to?(:request)
request
elsif respond_to?(:controller) && controller.respond_to?(:request)
controller.request
end
end
end
end
end
33 changes: 33 additions & 0 deletions actionpack/test/template/asset_tag_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def config
class AssetTagHelperTest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper

attr_reader :request

def setup
super
silence_warnings do
Expand Down Expand Up @@ -598,6 +600,8 @@ def test_caching_image_path_with_caching_and_proc_asset_host_using_request
class AssetTagHelperNonVhostTest < ActionView::TestCase
tests ActionView::Helpers::AssetTagHelper

attr_reader :request

def setup
super
@controller = BasicController.new
Expand Down Expand Up @@ -720,3 +724,32 @@ def test_assert_css_and_js_of_the_same_name_return_correct_extension
assert_dom_equal(%(/collaboration/hieraki/stylesheets/foo.css), stylesheet_path("foo"))
end
end

class AssetUrlHelperControllerTest < ActionView::TestCase
tests ActionView::Helpers::AssetUrlHelper

def setup
super

@controller = BasicController.new
@controller.extend ActionView::Helpers::AssetUrlHelper

@request = Class.new do
attr_accessor :script_name
def protocol() 'http://' end
def ssl?() false end
def host_with_port() 'www.example.com' end
def base_url() 'http://www.example.com' end
end.new

@controller.request = @request
end

def test_asset_path
assert_equal "/foo", @controller.asset_path("foo")
end

def test_asset_url
assert_equal "http://www.example.com/foo", @controller.asset_url("foo")
end
end

0 comments on commit c3cff4d

Please sign in to comment.