Permalink
Browse files

Fixed normalize_path in Routing::Mapper to handle optional prefix seg…

…ments with static and dynamic parts

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 7bd85a8 commit 59b24ceb0c558715714e272737ca0ca06f6043d5 @pjb3 pjb3 committed with josevalim Jun 26, 2010
Showing with 47 additions and 1 deletion.
  1. +1 −1 actionpack/lib/action_dispatch/routing/mapper.rb
  2. +46 −0 actionpack/test/dispatch/routing_test.rb
@@ -194,7 +194,7 @@ def default_controller
# for root cases, where the latter is the correct one.
def self.normalize_path(path)
path = Rack::Mount::Utils.normalize_path(path)
- path.sub!(%r{/(\(+)/?:}, '\1/:') unless path =~ %r{^/\(+:.*\)$}
+ path.sub!(%r{/(\(+)/?([^:]*):}, '\1/\2:') unless path =~ %r{^/\(+:.*\)$}
path
end
@@ -308,6 +308,10 @@ def self.matches?(request)
match "index", :to => 'private#index'
end
+ get "(/:username)/followers" => "followers#index"
+ get "/groups(/user/:username)" => "groups#index"
+ get "(/user/:username)/photos" => "photos#index"
+
match "whatever/:controller(/:action(/:id))"
resource :profile do
@@ -1466,6 +1470,48 @@ def test_resources_controller_name_is_not_pluralized
end
end
+ def test_url_generator_for_optional_prefix_dynamic_segment
+ with_test_routes do
+ get '/bob/followers'
+ assert_equal 'followers#index', @response.body
+ assert_equal 'http://www.example.com/bob/followers',
+ url_for(:controller => "followers", :action => "index", :username => "bob")
+
+ get '/followers'
+ assert_equal 'followers#index', @response.body
+ assert_equal 'http://www.example.com/followers',
+ url_for(:controller => "followers", :action => "index", :username => nil)
+ end
+ end
+
+ def test_url_generator_for_optional_suffix_static_and_dynamic_segment
+ with_test_routes do
+ get '/groups/user/bob'
+ assert_equal 'groups#index', @response.body
+ assert_equal 'http://www.example.com/groups/user/bob',
+ url_for(:controller => "groups", :action => "index", :username => "bob")
+
+ get '/groups'
+ assert_equal 'groups#index', @response.body
+ assert_equal 'http://www.example.com/groups',
+ url_for(:controller => "groups", :action => "index", :username => nil)
+ end
+ end
+
+ def test_url_generator_for_optional_prefix_static_and_dynamic_segment
+ with_test_routes do
+ get 'user/bob/photos'
+ assert_equal 'photos#index', @response.body
+ assert_equal 'http://www.example.com/user/bob/photos',
+ url_for(:controller => "photos", :action => "index", :username => "bob")
+
+ get 'photos'
+ assert_equal 'photos#index', @response.body
+ assert_equal 'http://www.example.com/photos',
+ url_for(:controller => "photos", :action => "index", :username => nil)
+ end
+ end
+
private
def with_test_routes
yield

0 comments on commit 59b24ce

Please sign in to comment.