aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorNicholas Seckar <nseckar@gmail.com>2005-09-11 13:56:43 +0000
committerNicholas Seckar <nseckar@gmail.com>2005-09-11 13:56:43 +0000
commit27962ead0380dcc3997eb45714d6334960f8cce7 (patch)
treef0128845facd2e506f6c53ca8853042734a94d7b /actionpack
parent9e5d64b3bbc9a1a4b15c4728d65dbce226ca9512 (diff)
downloadrails-27962ead0380dcc3997eb45714d6334960f8cce7.tar.gz
rails-27962ead0380dcc3997eb45714d6334960f8cce7.tar.bz2
rails-27962ead0380dcc3997eb45714d6334960f8cce7.zip
Add ability to specify Route Regexps for controllers. Closes #1917.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2205 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/routing.rb9
-rw-r--r--actionpack/test/controller/routing_test.rb38
3 files changed, 48 insertions, 1 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 2da874782d..c3a771e6d2 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add ability to specify Route Regexps for controllers. Closes #1917. [Sebastian Kanthak]
+
* Provide Named Route's hash methods as helper methods. Closes #1744. [Nicholas Seckar, Steve Purcell]
* Added :multipart option to ActiveRecordHelper#form to make it possible to add file input fields #2034 [jstirk@oobleyboo.com]
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index f06a464adb..c6cd8df6b4 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -192,7 +192,14 @@ module ActionController
g << "controller_result = ::ActionController::Routing::ControllerComponent.traverse_to_controller(#{g.path_name}, #{g.index_name})"
g.if('controller_result') do |gp|
gp << 'controller_value, segments_to_controller = controller_result'
- gp.move_forward('segments_to_controller') {|gpp| yield gpp, :constraint}
+ if condition
+ gp << "controller_path = #{gp.path_name}[#{gp.index_name},segments_to_controller].join('/')"
+ gp.if(Routing.test_condition("controller_path", condition)) do |gpp|
+ gpp.move_forward('segments_to_controller') {|gppp| yield gppp, :constraint}
+ end
+ else
+ gp.move_forward('segments_to_controller') {|gpp| yield gpp, :constraint}
+ end
end
end
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index cc8b50390b..0da376f519 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -238,6 +238,20 @@ class RecognitionTests < Test::Unit::TestCase
assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'hi'}, execute('hi/admin/user'))
end
+ def test_controller_with_regexp
+ c = [Static.new("hi"), Controller.new(:controller, :condition => /^admin\/.+$/)]
+ g.constant_result :action, "hi"
+
+ go c
+
+ assert_nil execute('hi')
+ assert_nil execute('hi/x')
+ assert_nil execute('hi/content')
+ assert_equal({:controller => ::Controllers::Admin::UserController, :action => 'hi'}, execute('hi/admin/user'))
+ assert_equal({:controller => ::Controllers::Admin::NewsFeedController, :action => 'hi'}, execute('hi/admin/news_feed'))
+ assert_nil execute('hi/admin/user/foo')
+ end
+
def test_standard_route(time = ::RunTimeTests)
c = [Controller.new(:controller), Dynamic.new(:action, :default => 'index'), Dynamic.new(:id, :default => nil)]
go c
@@ -407,6 +421,17 @@ not_expired = true
assert_equal '/hi/admin/user', execute({}, {:controller => 'admin/user'})
end
+ def test_controller_with_regexp
+ c = [Static.new("hi"), Controller.new(:controller, :condition => /^admin\/.+$/)]
+ go c
+
+ assert_nil execute({}, {})
+ assert_nil execute({:controller => 'content'}, {})
+ assert_equal '/hi/admin/user', execute({:controller => 'admin/user'}, {})
+ assert_nil execute({}, {:controller => 'content'})
+ assert_equal '/hi/admin/user', execute({}, {:controller => 'admin/user'})
+ end
+
def test_standard_route(time = ::RunTimeTests)
c = [Controller.new(:controller), Dynamic.new(:action, :default => 'index'), Dynamic.new(:id, :default => nil)]
go c
@@ -635,6 +660,19 @@ class RouteSetTests < Test::Unit::TestCase
$stderr = old_stderr
end
+ def test_route_with_regexp_for_controller
+ rs.draw do |map|
+ map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
+ map.connect ':controller/:action/:id'
+ end
+ assert_equal({:controller => ::Controllers::Admin::UserController, :admintoken => "foo", :action => "index"}.stringify_keys,
+ rs.recognize_path(%w(admin user foo)))
+ assert_equal({:controller => ::Controllers::ContentController, :action => "foo"}.stringify_keys,
+ rs.recognize_path(%w(content foo)))
+ assert_equal ['/admin/user/foo', []], rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
+ assert_equal ['/content/foo',[]], rs.generate(:controller => "content", :action => "foo")
+ end
+
def test_basic_named_route
rs.home '', :controller => 'content', :action => 'list'
x = setup_for_named_route