From 401426618495b673025b13cc64323c871e67aa0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Camargo?= Date: Sat, 16 Oct 2010 08:03:00 -0300 Subject: [PATCH] Fix RouteSet::Generator#use_relative_controller! to work with absolute controller path --- .../lib/action_dispatch/routing/route_set.rb | 14 ++++++-- actionpack/test/dispatch/routing_test.rb | 35 ++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 32f4193..bf5fbd5 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -419,12 +419,18 @@ module ActionDispatch # if the current controller is "foo/bar/baz" and :controller => "baz/bat" # is specified, the controller becomes "foo/baz/bat" + # or, if an absolute :controller => "/baz/bat" is specified, + # the controller becomes "baz/bat" def use_relative_controller! if !named_route && different_controller? - old_parts = current_controller.split('/') - size = controller.count("/") + 1 - parts = old_parts[0...-size] << controller - @controller = @options[:controller] = parts.join("/") + @controller = @options[:controller] = if controller[0,1] == '/' + controller[1..-1] + else + old_parts = current_controller.split('/') + size = controller.count("/") + 1 + parts = old_parts[0...-size] << controller + parts.join("/") + end end end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 5c188a6..e5c7676 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -2255,3 +2255,38 @@ class TestDefaultScope < ActionDispatch::IntegrationTest end end +class TestRouteSetGeneratorUseRelativeControllerMethod < ActionDispatch::IntegrationTest + module ::Foo + module Bar + class BazController < ActionController::Base + def relative + render :text => url_for(:controller => 'baz/bat') + end + def absolute + render :text => url_for(:controller => '/foobar') + end + end + end + end + + Routes = ActionDispatch::Routing::RouteSet.new + Routes.draw do + match 'foo/bar/baz(/:action)' => 'foo/bar/baz#index' + match 'foo/baz/bat' => 'foo/baz/bat#index' + match 'foobar' => 'foobar#index' + end + + def app + Routes + end + + def test_relative_controller_path + get '/foo/bar/baz/relative' + assert_equal "http://www.example.com/foo/baz/bat", @response.body + end + + def test_absolute_controller_path + get '/foo/bar/baz/absolute' + assert_equal "http://www.example.com/foobar", @response.body + end +end -- 1.7.1