6 messages in com.googlegroups.pylons-discussRe: Nested resources with map.resources?| From | Sent On | Attachments |
|---|---|---|
| wyatt-bC | 10 Feb 2007 19:31 | |
| Ben Bangert | 10 Feb 2007 21:36 | |
| wyatt-bC | 19 Feb 2007 19:12 | |
| wyatt-bC | 19 Feb 2007 19:26 | |
| Ian Bicking | 19 Feb 2007 20:41 | |
| Ben Bangert | 19 Feb 2007 22:28 |
| Subject: | Re: Nested resources with map.resources?![]() |
|---|---|
| From: | Ben Bangert (ben-...@public.gmane.org) |
| Date: | 02/19/2007 10:28:46 PM |
| List: | com.googlegroups.pylons-discuss |
On Feb 19, 2007, at 7:27 PM, wyatt-bC wrote:
One thing that's cool about the Rails version is that you can nest multiple resources at once inside the block:
map.resources :articles do |article| article.resources :comments article.resources :tags end
My question is, what sort of object is `article`? map.resource could return the `article` thingie, then the Python version could look like this:
article = map.resource('article', 'articles') article.resource('comment', 'comments') article.resource('tag', 'tags')
Or maybe a `with` version would work:
with map.resource('article', 'article') as article: article.resource('comment', 'comments') article.resource('tag', 'tags')
This at least looks similar to the Rails version (though maybe not a "proper" use of `with`?).
Unfortunately neither of this will quite work. The Rails one is utilizing the fact that under Rails, the block isn't really executing when you think it is. The with statement maintains control of loop execution, while in Ruby, the contents of the block are at the mercy of the function taking it.
Thus with Rails, the map.resources command has the capability to take a block in addition to its resource that its mapping, and can then call the contents of the block with article capturing the statement and executing the necessary wrapping (the additional keyword arguments I showed in the map.resource command). Rails Routing does a similar thing to let you declare a set of routes that all take another argument without actually writing it out each time.
So my original example (which should work): map.resource('comment', 'comments', controller='article_comments', name_prefix='article_', path_prefix='articles/:article_id')
When you do in Rails:
map.resources :articles do |article|
It sets up the articles resource, then sets up a set of arguments (name_prefix='article_', path_prefix='articles/:article_id', controller='article_' + your_controllers_name) that it will then call map.resource on again (thus letting you keep nesting them with Ruby anonymous blocks).
As Python has no anonymous blocks, and the with statement comes nowhere near them, its pretty much impossible to replicate such functionality with the ease its done in Ruby. One thing that can perhaps be done, is to return a special function that calls map.resource with such arguments and modifies the controller name as needed. It would almost be like a partial function (new in Python 2.5) though since it actually has to modify one of the arguments (the controller arg), not quite.
As map.resource is not currently expected to return anything, I see no harm in having it return such a function, though it seems awfully hackish to me for some reason....
Cheers, Ben




