This project is archived and is in readonly mode.
add support for shallow nesting of resource routes
-
RSL
Or [and I'm going on a limb here] you could just not make that resource nested so deep. http://weblog.jamisbuck.org/2007...
-
S. Brent Faulkner
Actually that is the purpose of the selective routes.
Jamis's proposed solution to the problem winds up doubly mapping the routes... so there is one route for /projects/1/milestones/2 and a second for /milestones/2
By selectively mapping them, the show_milestones route exists... the show_project_milestones routes does not.
(and my example was an exageration on purpose ;-)
-
RSL
I admit I was blinded by that last URL. If the goal of this is to DRY up the routing there while encouraging a more direct routing where possible, I'd like to see something more akin to
map.resources :users do |user| # Yes, this is rather hideous. user.resources :comments, :nest => [:collection, :new], :flat => [:member] endor
# A little better. ;) map.resources :users, :nest => {:comments => [:collection, :new]Apologies for my completely kneejerk reaction to seeing "nested resource" and that last URL. ;)
-
S. Brent Faulkner
How about using a notation similar to that used for ActiveRecord#find :includes
We could then use...
map.resources :projects => { :milestones => { :features => :tasks } }Instead of the deeply nested blocks of my previous example.
Then, to get the flattening, we could just add a boolean option...
map.resources :projects => { :milestones => { :features => :tasks } }, :flatten => truePersonally, I wouldn't mind the :flatten option defaulting to true and requiring :flatten => false to get the "legacy" style of routes.
Thoughts?
-
S. Brent Faulkner
Hmmm... just remembered the addition of has_many and has_one...
So, I guess I'm suggesting the following...
map.resources :projects, :has_many => { :milestones => { :features => :tasks } }, :flatten => trueAgain, I guess the default could be to flatten, but this would mean that we're changing an existing behaviour for the simple case...
map.resources :posts, :has_many => :comments(which I don't think is a good idea -- ie. changing the existing behaviour -- so, I can live with needing to explicitly specify :flatten => true :-)
I think I'll try and build up a patch for this version instead...
-
S. Brent Faulkner
OK... how about this? I've attached a new patch for resource mapping (and appropriate tests) that implements (optional) shallow nesting for resources.
This allows you to specify :shallow => true for a resource. The option is inherited by any nested resources and causes any member routes (those with an id parameter) to not require the parent path prefix.
So, you can now...
map.resources :projects, :shallow => true do |project| project.resources :milestones do |milestone| milestone.resources :features do |feature| feature.resources :tasks end end endor...
map.resources :projects, :has_many => { :milestones => { :features => :tasks } }, :shallow => trueMuch nicer than my original proposal.
In either case, I'd be able to access resources with paths like the following...
/projects /projects/1 /projects/1/milestones /milestones/2 /milestones/2/features /features/56/tasks/new
(no redundant parent_id parameters... just what's required)
-
S. Brent Faulkner
- Title changed from add support for selective resource routes to add support for shallow nesting of resource routes
-
DHH
Really nice. Shallow is a great term for it too. It needs documentation, though. Then I'd say it would be ready to go. Nice work.
-
S. Brent Faulkner
Thanks again, David.
I've attached a new patch with:
a) documentation
b) the support for passing deep :has_many information using a hash/array
for example:
map.resources :projects, :has_many => { :milestones => { :features => :tasks } }, :shallow => true -
S. Brent Faulkner
regarding (b) ... I had actually thought that this was already supported, but must have been thinking of the ActiveRecord finder :include option, so added it in since that was was part of the beauty of this solution
-
S. Brent Faulkner
tests pass, documentation included, please consider for commit
(hey, dhh thought it was ok :-)
-
S. Brent Faulkner
rebased and updated patch
-
S. Brent Faulkner
patch would not apply... fixed... uploaded new patch.
-
Pratik
- Assigned user set to Pratik
-
S. Brent Faulkner
I saw a recent commit to resources.rb and tested the patch. It wouldn't apply again, so I've rebased and attached the new one...
-
Repository
- State changed from new to resolved
(from [83c6ba18899a9f797d79726ca0078bdf618ec3d4]) Add support for shallow nesting of routes. [#838 add support for shallow nesting of resource routes state:resolved]
Adds :shallow option to resource route definition. If true, paths for nested resources which reference a specific member (ie. those with an :id parameter) will not use the parent path prefix or name prefix.
Example :
map.resources :users, :shallow => true do |user| user.resources :posts end
-
GET /users/1/posts (maps to PostsController#index action as usual) named route "user_posts" is added as usual.
-
GET /posts/2 (maps to PostsController#show action as if it were not nested) Additionally, named route "post" is added too. http://github.com/rails/rails/co...
