# Code dupplication?

**URL:** <https://www.ruby-forum.com/t/code-dupplication/114225>\
**Category:** Rails\
**Created:** [September 10, 2007, 8:15am UTC](https://www.ruby-forum.com/t/code-dupplication/114225 "2007-09-10T08:15:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 10, 2007, 8:15am UTC](https://www.ruby-forum.com/t/code-dupplication/114225/1 "2007-09-10T08:15:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 10, 2007, 8:21am UTC](https://www.ruby-forum.com/t/code-dupplication/114225/2 "2007-09-10T08:21:00Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 10, 2007, 8:38am UTC](https://www.ruby-forum.com/t/code-dupplication/114225/3 "2007-09-10T08:38:20Z")

</div>

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 🙂

@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 😃

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 10, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/code-dupplication/114225/4 "2007-09-10T14:12:58Z")

</div>

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

and then later on reactor it 🙂

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 13, 2007, 9:23am UTC](https://www.ruby-forum.com/t/code-dupplication/114225/5 "2007-09-13T09:23:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [September 10, 2007, 8:24am UTC](https://www.ruby-forum.com/t/code-dupplication/114225/6 "2007-09-10T08:24:22Z")

</div>

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 🙂

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.
