Thanks to the folks who suggested solutions to this question. I was trying to look deeper under the covers than I needed to and didn't see the obvious in front of my face. At first, I was suspicious of trusting a derived string for my answer, but then I looked into ActionController::Base and saw the code there doing a similar thing. Since methods like controller_class_name, controller_name and controller_path all depend ultimately on the class name itself, I'll go with controller.class.name and split out the modules as needed.

As it turns out, I've backed away from the refactoring I had intended to do, so I don't need that particular code right now (no doubt it will be useful in the future). Part of the reason of the refactoring was to make my code more RESTful. This is probably the 3rd or 4th time I have decided to do this, but each time I come to the same conclusion: it's not worth it.

My problem, I realise, is with generalising named routes. For me, it seems writing, for example:

redirect_to :action ="" :list

which is able to infer the controller, is easier than

redirect_to(some_model_url)

which needs the controller context to be explicitly built into the route name. When it comes to generalising some controller code that can handle any model, the named route solution is slightly messier.

I'm wondering if I get branded an apostate for not being sold on RESTfulness but since I already have a standard set of actions in an abstract controller class (which my 'concrete' controllers simply inherit from), it seems that going the whole RESTful route adds little value.

Any thoughts?

thanks in advance

Mark

P.S. the fruits of this current work I've assigned myself will soon be appearing on a github near you in the form of some plugins and 2.3 Engines. More later :-)

David Lee wrote:
do you want to operate on the class/module constant or the url path?

how about controller.class.to_s.split('::') # pop off the last one if you just want its parents
?

On Thu, Mar 19, 2009 at 1:23 PM, John Barton <jrbar...@gmail.com> wrote:

We sometimes use

controller.controller_path.split('/')[0]

to determine if we're in a namespaced subdir (usually for setting nav
bar stuff up)

-jb

On Mar 18, 1:56 pm, Clifford Heath <clifford.he...@gmail.com> wrote:
> On 18/03/2009, at 1:51 PM, Mark Ratjens wrote:
>
> > I'm in the midst of refactoring some code and I want to be able to
> > generically discover the module of a given controller (essentially  
> > so I
> > can dynamically build named routes rather than have them hard-coded  
> > into
> > each controller).
>
> Facets has Module.modspace, defined:
>
> class Module
>    def modspace
>      space = name[ 0...(name.rindex( '::' ) || 0)]
>      space == '' ? Object : eval(space)
>    end
> end
>
> Clifford Heath.




--
cheers,
David Lee



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To post to this group, send email to rails-oceania@googlegroups.com
To unsubscribe from this group, send email to rails-oceania+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to