Friday, June 6, 2008

List all Controllers and actions

controllers = Dir.new("#{RAILS_ROOT}/app/controllers").entries
controllers.each do |controller|
if controller =~ /_controller/
cont = controller.camelize.gsub(".rb","")
puts cont
(eval("#{cont}.new.methods") -
ApplicationController.methods -
Object.methods -
ApplicationController.new.methods).sort.each {|met|
puts "\t#{met}"
}
end

end

2 comments:

  1. Elad, there's a problem with this code snippet...
    If a controller has an action (e.g. "new") that is also in ActionController, then that action is not listed by this script. At this moment I'm not sure what the solution is. But I thought I'd mention it in case anyone else stumbles across this post.

    ReplyDelete
  2. That's right... thankx for pointing that out.

    the solution is to handle application_controller seperatly from the other controllers, i am sure you can figure out from this code how to do it.
    if not, you can always ask me :)

    ReplyDelete

Tell me what you think