# DRY - with modules, render\_component or..?

**URL:** <https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837>\
**Category:** Rails\
**Created:** [March 23, 2007, 2:11am UTC](https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837 "2007-03-23T02:11:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 23, 2007, 2:11am UTC](https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837/1 "2007-03-23T02:11:13Z")

</div>

I have an B2B application where a pretty complex order form needs to be  
submitted and edited on the admin controller, buyer controller and  
seller controller with some small differences.  
How do I make available the edit order methods all controllers?

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 23, 2007, 7:00am UTC](https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837/2 "2007-03-23T07:00:56Z")

</div>

Hi Constantin -

Why don’t you just have a order\_controller.rb that establishes/follows  
permissions based on whether the user is an admin, buyer or seller?

Marc

On Mar 22, 6:11 pm, Constantin G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 23, 2007, 7:52am UTC](https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837/3 "2007-03-23T07:52:31Z")

</div>

Marc L. wrote:

> Hi Constantin -
> 
> Why don’t you just have a order\_controller.rb that establishes/follows  
> permissions based on whether the user is an admin, buyer or seller?

I thought about that, even created the controller, moved all the code in  
it, but every role needs a different layout. Not only the layout, but  
additional code needs to be in the controller for edit\_order/list\_orders  
actions, code that does not have anything to do with the order, so it  
would not make any sense to put it in the order\_controller.

I’m trying to make a module now, called manage\_orders, and I’m planning  
to keep the template files in shared/order/\*.rhtml  
Another alternative would be just to dump the methods into  
ApplicationController.

I am wondering if there is any way that’s more beautiful or elegant to  
share this common code between controllers.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 23, 2007, 7:57am UTC](https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837/4 "2007-03-23T07:57:00Z")

</div>

The components route was tried but apparently had negative performance  
attributes. For this and probably other reasons, Rails Core is not  
advocating use of components. However, plugins will fill the same bill.

Any extension you make to ActionController in your plugin will be seen  
by  
all controllers, and it sounds like that’s the only place where you need  
to  
make changes. The one additional wrinkle is where the template goes. I  
recommend putting the template in the plugin tree as well in a views  
directory, then changing telling Rails where to find the correct view.  
You  
do that by changing template\_root in earlier versions of Rails and by  
calling class function prepend\_view\_path in edge. Here’s an example  
(untested, sorry):

class ActionController  
prepend\_view\_path  
“#{RAILS\_ROOT}/vendor/plugins/my\_plugin/lib/app/views”

def new\_order  
@order = Order.new # should look for template in your  
plugin/lib/app/views directory  
end

# etc.

end

Hope this helps.

Mike-293 wrote:

> >

–  
View this message in context:  
[http://www.nabble.com/DRY---with-modules%2C-render\_component-or](http://www.nabble.com/DRY---with-modules%2C-render_component-or)…—tf3451425.html#a9629098  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [March 25, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/dry-with-modules-render-component-or/89837/5 "2007-03-25T21:25:04Z")

</div>

I’ve put the “order actions” in the order\_actions.rb file in the models  
directory and I’m including it in the Admin and Customer controllers as  
a mixin.  
It seems to work.

class AdminController \< ApplicationController  
require “order\_actions”  
include OrderActions  
end

===order\_actions.rb===  
Module OrderActions  
def list\_orders  
@orders = Order.find(:all)  
raise(‘test’) #this is never raised; why?  
# RENDER IS IGNORED  
render :template =\> ‘shared/order/list\_orders’  
end  
end

Steve R. wrote:

> The components route was tried but apparently had negative performance  
> attributes. For this and probably other reasons, Rails Core is not  
> advocating use of components. However, plugins will fill the same bill.
> 
> Any extension you make to ActionController in your plugin will be seen  
> by  
> all controllers, and it sounds like that’s the only place where you need  
> to  
> make changes.
