Riley
2006-Apr-04 17:06 UTC
[Rails] Sharing controller code between views - best practices?
Hi, I have controller code that needs to be shared between multiple different user type views and want to know what is considered ''best practice'' or what other people are doing out there. Example: I have admin users, salesperson users, and possibly another type of user. They all need code to add/edit/delete a property - and other abilities. The code would be identical(in most cases) but needs to forward to different views. The wrong way to do this would seem to be duplicating this code across multiple controllers. Should this code be placed in a parent classes protected methods and the subclass only contain public methods which have calls to these methods and view logic code? Or should this code be in a module? Or should there be a single controller that figures out the user type and goes to the appropriate view based on that? Or is there another solution? Thanks ahead of time for any answers.
Gregory Seidman
2006-Apr-04 18:19 UTC
[Rails] Sharing controller code between views - best practices?
On Tue, Apr 04, 2006 at 10:06:38AM -0700, Riley wrote: } I have controller code that needs to be shared between } multiple different user type views and want to know } what is considered ''best practice'' or what other } people are doing out there. } } Example: I have admin users, salesperson users, and } possibly another type of user. They all need code to } add/edit/delete a property - and other abilities. The } code would be identical(in most cases) but needs to } forward to different views. The wrong way to do this } would seem to be duplicating this code across multiple } controllers. } } Should this code be placed in a parent classes } protected methods and the subclass only contain public } methods which have calls to these methods and view } logic code? Or should this code be in a module? Or } should there be a single controller that figures out } the user type and goes to the appropriate view based } on that? Or is there another solution? I created a module for the purpose. While you''re developing it you can put it in the application.rb file (which gets reloaded with every request in dev mode), but it probably belongs in a file in lib. } Thanks ahead of time for any answers. --Greg
Thiago Arrais
2006-Apr-04 18:25 UTC
[Rails] Sharing controller code between views - best practices?
On 4/4/06, Riley <r_stonehouse@yahoo.com> wrote:> Should this code be placed in a parent classes > protected methods and the subclass only contain public > methods which have calls to these methods and view > logic code?I am not sure I like this one.> Or should this code be in a module?With this one you could end-up with a lot of delegating-only, duplicate code.> Or > should there be a single controller that figures out > the user type and goes to the appropriate view based > on that?Would you give us some more detail on this one? I am not sure I understand your idea.> Or is there another solution?Disclaimer: just brainstorming here, bad ideas are likely to happen... Maybe you could separate the controllers in terms of end-user functionality and have different methods for each user type. Features seam to (mostly) not change based on user type, but views do. Maybe you could have the user type be figured by the controller and it can choose to render the appropriate view. Like this: ---- class AlmightyController < ActiveController::Base def do_something user_type = retrieve_user_type(params[:user_id]) render(:action => user_type) end end ---- You just need to name your templates after the user type codes. Cheers, Thiago Arrais