I am upgrading my Rails plugin to be an engine that works with the latest 3.0RC1 release and I'm having a bit of trouble figuring out the best (and most correct) way to extend ActionController
. I've seen this post by DHH and this question here on SO, but my question is more about how to properly call code within the ActionController
.
For instance, I need to call the following within my engine's controller:
class ApplicationController < ActionController::Base
helper :all
before_filter :require_one_user
after_filter :store_location
private
def require_one_user
# Code goes here
end
def store_location
# Code goes here
end
end
I know how to properly include my two private functions, but I can't find a way to get it to properly call helper
, before_filter
and after_filter
.
I would greatly appreciate some links or a way to make this work. I have tried naming my controller something other than ApplicationController
and having the real ApplicationController
extend it, but that doesn't seem to work either. I'm really up for any solution that makes the life of the engine user as easy as possible. Ideally, they wouldn't have to extend my class, but they'd have all of the functionality built into their own ApplicationController
.
You may also want to look into the initializers inside your engine subclass, so you don't have to include view helpers inside your controller class. And this will give you control over the load order of these modules.
Here is what I have been using:
Also, another option for the action controller extension is using a mixin module. This will let you use the before_filter, after_filter, etc..
One other thing... if you create the default rails directories at the top level of your gem, you don't have to worry about requiring the helpers or controllers. Your engine subclass has access to them. So I add my application controller and application helper extensions here:
I like this setup because it looks similar to the application_controller and application_helper in your rails app. Again, this is just personal preference, but I try to keep anything that is directly rails related, such as controllers, helpers and models inside /my_engine/app and anything that is related to the plugin in general inside /my_engine/lib
Check out this tutorial by Jose Valim for more info on initializers: https://gist.github.com/e139fa787aa882c0aa9c (engine_name is deprecated now, but most of this doc seems up-to-date)
So, I finally figured out the solution and I hope it helps someone else.
You need to create a file in your lib directory because you are actually going to extend the class. I did
myplugin/lib/extensions/action_controller_base.rb
.Then, inside of your
myplugin/lib/myplugin.rb
file, do the following:Inside of
myplugin/lib/extensions/action_controller_base.rb
put the following:If you need to have view helpers, create them in the
myplugin/lib/helpers
directory (or anything inside of lib, the name "helpers" doesn't matter) also and add the following to the bottom ofmyplugin/lib/extensions/action_controller_base.rb
: