Working on a super secret stealth project last night, I needed an **Array** of all the actions on a specific controller. I figured it would be a piece of cake given the introspection support in rails. And after scouring the rails source code for *minutes*, I concluded that it wasn’t supported with a single method. And it becomes even trickier when you try to get it from a class method. Take this example:
Class AuthorController < ApplicationController
acts_as_super_secret
def index
render text => "find me"
end
end
There is no possible way from inside the implementation of **acts\_as\_super\_secret** to know that the **index** action is available on the controller. This is due to the fact that ruby calls **acts\_as\_super\_secret** as it is parsing the class. So, you need to get a little fancier. It is a little beyond the scope of this post to give the whole solution, but here is the code I used to find the actions on a controller:
AuthorController.new.public_methods -
ApplicationController.new.public_methods
That needs to be run after the class is fully loaded. I am sure you could pull it out into something more efficient and generic:
def ApplicationController < ActionController::Base
def actions
self.public_methods - self.class.superclass.new.public_methods
end
end
Now you can call actions on or from any controller to get a list of actions available in that controller.
Hey Nicholas!
Did you try the
action_methodsmethod defined on the ActionController::Base class? Here’s some sample output from a Rails app I’m currently working on:Hope this helps!
It appears you are correct! I guess I should have done a little more research