Forum: Ruby on Rails Contextually aware controllers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3cb5aadfd1626e37759e987c7184e372?d=identicon&s=25 Brendon Muir (spike)
on 2009-02-27 02:59
(Received via mailing list)
Hi everyone, my CMS app is starting to get quite large. I have heaps
of modules (about 15) and some of these modules are re-used within
different contexts:

An example is that you can add a Page to the schools website, but you
can also add a page to a 'Learning Cave' which is our online learning
area for kids. While all the controller and view code is essentially
the same, we have it spread out (or copied) into two seperate
controllers because the layout for a Learning Cave is different to the
layout in the admin area where teachers add pages. Anyway, what I'm
wanting to be able to do is know the context in which my controller is
being called and use this to decide whether to run certain filters,
and also to decide the layout to use and other small things. This way
I can combine all the controller code into one controller but call it
from multiple places and have it respond differently depending on the
context. Here's an example:

/admin/pages/new - shows the new page form using the layout for the
admin area (might also do some permission checking on the side that's
only relevant to the admin area)

/learning_cave/pages/new - shows the same view and uses the same
controller but uses a different layout and possibly some other checks
that are only relevant for caves.

I've been attempting to do this with restful routing and
polymorphic_path (because I won't know the context ahead of time) but
I'm having no luck. Does anyone know if there is already anything out
there that would assist with this approach? I'm trying to package all
my modules as rails engines and something like this would help
greatly.

Cheers,

Brendon
This topic is locked and can not be replied to.