Forum: Ruby on Rails Sharing controller code between views - best practices?

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.
89a4bc7f7736e9ddde5ba797c6751d5e?d=identicon&s=25 Riley (Guest)
on 2006-04-04 19:08
(Received via mailing list)
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.
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-04 20:21
(Received via mailing list)
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
B1d59a804bd67487c964bc505a8eb892?d=identicon&s=25 Thiago Arrais (Guest)
on 2006-04-04 20:27
(Received via mailing list)
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
This topic is locked and can not be replied to.