Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Routes can be selectively namespaced by path or controller module

  • Loading branch information...
commit ac0280c39dab272710c25d54810b21c10fff9c52 1 parent e77a031
@jeremy jeremy authored
View
19 actionpack/CHANGELOG
@@ -1,4 +1,21 @@
-*Rails 3.0.0 [Edge] (pending)*
+*Rails 3.0.0 [beta 3] (pending)*
+
+* Routes can be selectively namespaced by path or controller module. [Jeremy Kemper]
+
+ # /users => Admin::UsersController
@rishav
rishav added a note

isnt this going to be a bit weird ?

@jeremy Owner
jeremy added a note

Useful for mounting a plugin's namespaced controllers at toplevel. Equivalent to scope(:controller_namespace => 'admin')

@nate
nate added a note

I think that the key for the option is :controller is strange, myself. I don't know what would be better, however.

@jeremy Owner
jeremy added a note

It's namespacing the controller. :module is another option; stuck with :controller for consistency.

@nate
nate added a note

Maybe :constant instead?

@wycats Collaborator
wycats added a note

I actually really like :module -- it's very obvious what it's doing

@nate
nate added a note

That assumes that it's a module, which may not be the case, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ namespace :controller => 'admin' do
+ resources :users
+ end
+
+ # /admin/users => UsersController
+ namespace :path => 'admin' do
+ resources :users
+ end
+
+ # /admin/users => Admin::UsersController
+ namespace :admin do
+ resources :users
+ end
* Added #favicon_link_tag, it uses #image_path so in particular the favicon gets an asset ID [fxn]
View
17 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -323,8 +323,21 @@ def controller(controller)
scope(controller.to_sym) { yield }
end
- def namespace(path)
- scope(path.to_s, :name_prefix => path.to_s, :controller_namespace => path.to_s) { yield }
+ def namespace(path_or_options)
+ options =
+ case path_or_options
+ when String, Symbol
+ path = path_or_options.to_s
+ { :path => path,
+ :name_prefix => path,
+ :controller_namespace => path }
+ when Hash
+ { :path => path_or_options[:path],
+ :controller_namespace => path_or_options[:controller] }
+ else
+ raise ArgumentError, "Unknown namespace: #{path_or_options.inspect}"
+ end
+ scope(options) { yield }
end
def constraints(constraints = {})
View
24 actionpack/test/dispatch/routing_test.rb
@@ -186,6 +186,14 @@ def self.matches?(request)
end
resource :dashboard, :constraints => { :ip => /192\.168\.1\.\d{1,3}/ }
+
+ namespace :controller => :api do
+ resource :token
+ end
+
+ namespace :path => :api do
+ resource :me
+ end
end
end
@@ -942,6 +950,22 @@ def test_resource_constraints
end
end
+ def test_controller_namespace
+ with_test_routes do
+ get '/token'
+ assert_equal 'api/tokens#show', @response.body
+ assert_equal '/token', token_path
+ end
+ end
+
+ def test_path_namespace
+ with_test_routes do
+ get '/api/me'
+ assert_equal 'mes#show', @response.body
+ assert_equal '/api/me', me_path
+ end
+ end
+
private
def with_test_routes
yield

3 comments on commit ac0280c

@radar

I think I'm going to see if this works with root routes now and if so squee with joy. If not...

@jeremy
Owner

How would that work, radar? It'd collide with the toplevel root's named route. Use match '/' => ... instead.

@radar

match '/' is good enough. Thanks!

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