DRY - with modules, render_component or..?

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?

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-

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.

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…—tf3451425.html#a9629098
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

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.