Controllers reuse or the way to reuse code along with views

Consider the following scenario:

  1. In a web application users are given an ability to customize what
    is displayed on their homepage.

  2. All options they get for customization are already implemented as
    separate pages server by different controllers.

  3. Now I would like to have some super-method available at fixed URL
    which will decide based on user settings which page to serve to the
    user and call the appropriate controller.

Now of course I will be told that it’s violation of main doctrines and
basic principles :slight_smile: and I’m not supposed to do that, and I agree to
some extent (seems like someone already did this anyway
http://www.railsonwave.com/2008/10/25/how-to-call-a-controller-s-action-from-a-different-controller/
).

But this in my thinking brings another question - how to reuse just
code along with views? There is Cells plugin
GitHub - apotonick/cells_examples: A rails project to demonstrate cells in action. but I’m sort of
wary about bringing this in, especially since ActionControllers
already have what I need, i.e. the code with attached views.

Moving the shared code to the module will work but only to the some
extent since there will be no conventional place for templates, no
simple way to define filters, etc.

Probably there is something I’m missing out?

TIA,
Evgeniy

Evgeniy D. wrote:

  1. Now I would like to have some super-method available at fixed URL
    which will decide based on user settings which page to serve to the
    user and call the appropriate controller.

Personally, I would just write a controller that, after choosing the
proper controller, just redirects to the URL for that controller. Any
reason not to do that?

But this in my thinking brings another question - how to reuse just
code along with views? There is Cells plugin
GitHub - apotonick/cells_examples: A rails project to demonstrate cells in action. but I’m sort of
wary about bringing this in, especially since ActionControllers
already have what I need, i.e. the code with attached views.

Moving the shared code to the module will work but only to the some
extent since there will be no conventional place for templates, no
simple way to define filters, etc.

I’m not quite sure what you mean? There’s no official built-in way to
re-use code amongst controllers. But you can use any OO technique you
want. Sure, you can put it in a module. Since you’ll be including the
module in existing controllers, conventional place for templates and
filters etc will remain pretty much the same. Code in a module ends up
getting executed ‘as if’ it was in the class that included it, right?
It’ll still look for templates in the same places as usual. If you have
views shared between different controllers, just make a ‘shared’ sub-dir
in your views dir, and refer to the template specifically as
‘shared/template_name’ instead of just ‘template_name’. Or you can call
it anything you want, not just ‘shared’. This technique works for
sharing view templates between controllers regardless of whether you
need to share logic between controllers or not.

I think I’d use a module for shared code between controllers. You could
also use a common superclass for multiple controllers, but I don’t think
I’d do that. You could also put common code in an entirely seperate
library class, that the controller gets a reference to:
@more_controller_logic = MyClass.new ;
@more_controller_logic.do_something. Depending on what it is you want
to do, that may or may not be convenient.

I think a module is probably what you want. If there’s a specific kind
of logic you want to put in your module shared between controllers that
for some reason doesn’t seem to be working… try asking with that
specific example. I think a module should work fine.

Jonathan