Code dupplication?

Hello,

I’m building a small website, where users can subscribe by filling a
form (like this forum) but can also be subscribed by the administrator
directly in the administrator zone.
The workflow is completly different depending of how the subscription
has been made (for instance, is the user is entered by the
administrator, no confirmation email is sent). That means that the
controlers will be a little bit different but not completly (there are
parts of the controlers which will logically be the same), and the same
will happen in the views.

Because I don’t want to dupplicate code, I’d like to know which is the
“usual” way to deal with that kind of situation. I think that I’ll use
some partial templates for the views, but what about the controlers?

Thanks a lot

I’d avoid premature extraction at all costs, a little duplication in
the end can be much easier to deal with.

If you keep in mind that most of the complicated model stuff should be
dealt by the model and not the controller you’ll probably have a very
slim controller in which case the duplication will be minimal.

You can always share the views if they are too big or change too
often, but since they’re essentially two different parts of the
application I’d just treat them as such.

Just my two cents…

On Sep 10, 3:15 am, Stefano G. [email protected]

Thanks for your answers!

What about creating a module (UsersController for instance) which would
contain all the generic stuff, and then include it in both of the
subscription and the administration controller ? Wouldn’t that reduce
the duplication? Then the controllers would overload the method they
need to be different and everybody is happy :slight_smile:

@jamal : Basically, selecting the action based on the type of user is
what I do. But I’d like to keep some coherence in the place I put my
methods. I mean, it would make no sense to put all the administration
stuff EXCEPT the users part in the controller of the administration, as
it would make no sense to allow some untrusted user to access the
administration controller for subscription … Or at least it’s my way to
see it :smiley:

You can do it in many different ways, so I would recommend doing it as
the way you like it :slight_smile:

and then later on reactor it :slight_smile:

Hello again,

I’m facing a similar problem again.
In my administration’s area I have to deal with elements (Users,
Products, …) which could share a lot of code (I mean, it’s almost the
same thing to list Users and Sold Products), so I’m getting a little bit
frisky about simply duplicating the UserController several times with
only little changes.
Basically what I’d like to do is :
Create an “ElementController” module containing all the generic stuff,
then make UserController,ProductController load it and overload the
“too-generic” methods, and finally load one of those controllers in
AdministrationController depending of the URL.
Unhopefully I know it doesn’t work that way in rails (I tried :D), could
you explain me how it is supposed to be done?

Thanks a lot

You will have two different type of users, (admin, and user), you can
use that to determine if you want to sent a mail or not :slight_smile:

And for the view you wouldn’t need to duplicate the form, you can put
your form under shared folder.

shared/_form.rhtml

and then use it in admin and and users.

But the creation in the controller would be duplicated.