Mountable Engines and ApplicationControllers

Scott Johnson
Sat, 05 May 2012 06:24:22 -0000

I''ve been messing around with mountable engines and I crossed a dilemma
that I am sure you faced when implementing them, what should
ApplicationController inherit from?  Today each engine has a unique, name spaced
ApplicationController, all of which inherit from ActionController::Base, then
all of the controllers in each engine inherit from their corresponding
ApplicationController.  I understand the need to set up the engines this way so
that they can run outside of the main_app, but all of the controllers in Rails
inherit from ApplicationController because they need to share code.  What
primarily comes to mind are authentication methods and session variables, i.e.
current_user.  So when creating a Rails app that uses mountable engines, it is
nice for all of the things to access  a common ancestor.  One solution is to
include modules in ActionController::Base, as it is a common ancestor to all the
controllers in the app.  This process is outlined in this blog:

http://www.cowboycoded.com/2011/02/11/working-with-applicationcontroller-in-a-rails-3-engine/

While this works it has in my opinion two problems --
1) there is not a convention on where to put the module that gets included on
ActionController::Base
2) because the module gets included on initialization, you have to restart your
Rails app for any changes to take effect

What I have done to combat these issues is create an EnginesController that
inherits from ActionController::Base, then have each of my engines''
ApplicationControllers inherit from EnginesController.  This seems to solve the
above issues as long as EnginesController is in the load path.

So my questions for the group are, does this seem like a reasonable solution or
am I missing something like, this is why ActionController::Base exists and why
don''t you just open class it in the load path for the same effect?  And
if this is a reasonable approach, is it worth considering a convention on where
to put files that are shared among mountable engines and the main_app?

Thanks for your time and thoughts,


Scott