DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Generate A List Of Rails Controllers And Methods

  • submit to reddit
        This code goes over the App/controllers directory and extracts a list
of all controllers and their methods. 

use this script from rails console. 

I have used this script to generate a migration file with default authorization roles & rights.

controllers ="#{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 - {|met| 
       puts "\t#{met}"


to use it to create a Right object I used the following adjustments:

controllers.each do |controller|
    if controller =~ /_controller/ 
    name = controller.camelize.gsub(".rb","")
     (eval("#{name}.new.methods") - 
       Object.methods - {|met| 
          name_short = name.gsub("Controller","").downcase
          #it is possible to call the create directly from this script
          #I wanted to review and revise the names. 
          puts "#{met}_#{name_short}=Right.create(:name=>\"#{met} #{name_short}\",



Carla Brian replied on Sat, 2012/07/21 - 11:06pm

I hope  I can search an algorithm on this. I kinda don't know yet how to construct with this one. I will research more on this. - Mercy Ministries

Snippets Manager replied on Mon, 2009/06/01 - 12:00pm

Controller names: <%= ActionController::Routing::possible_controllers.inspect %> ["rails/info", "rails_info", "my_controller1", "my_controller2", "",..., ""] that could be then transformed to "MyController1", "MyController2" Action names for specific controller: <% Module.class_eval("MyController").action_methods.each do |action_method| -%> <%= action_method.to_s %> <% end -%> "my_action1" "my_action2" ... "my_last_controller_action"

Alexander Danilenko replied on Tue, 2009/04/28 - 10:24am

In some cases routes may be a better source for the list of controller actions. I use this code in my app: @controller_actions = ActionController::Routing::Routes.routes.inject({}) do |controller_actions, route| (controller_actions[route.requirements[:controller]] ||= []) << route.requirements[:action] controller_actions end

Snippets Manager replied on Fri, 2009/02/13 - 6:36am

this one works with rake: controller.camelize.gsub(".rb","") { |action|

Snippets Manager replied on Sun, 2009/01/25 - 10:07pm

This is a great module. thanks for providing it for me.

Francisco Viramontes replied on Tue, 2008/07/01 - 8:23pm

This works in recen rails versions mucho better desc "Load rights from controllers in FS" task :add_rights_to_db => :environment do |t| controllers ="#{RAILS_ROOT}/app/controllers").entries controllers.each do |controller| if controller =~ /_controller/ controller.camelize.gsub(".rb","") { |action| if Right.find_all_by_controller_name_and_controller_action(controller.camelize.gsub(".rb","").constantize.controller_path, action).empty? => controller.camelize.gsub(".rb","").constantize.controller_path, :controller_action => action).save end } end end end

Snippets Manager replied on Wed, 2008/02/06 - 6:53pm

Not sure if this helps anyone but this is from the defunct User Engine - I'm working on building a new simple RBAC system for a small app and used this to build my controller/action permission pairs. I call it from a rake task. # Find the actions in each of the controllers, ApplicationController.subclasses_of(ApplicationController).collect do |controller| { |action| if find_all_by_controller_and_action(controller.controller_path, action).empty? => controller.controller_path, :action => action).save end } end Note: action_method_names just calls action_methods but that's a private method so you have to do something like put it in your application.rb file. (worked for me in a pinch)

Snippets Manager replied on Tue, 2007/12/11 - 1:12pm

THX for this nice script !!!! I want to use in my app for authentication. However I have a quick question since I'm a Rails rookie: how can I build the model from the name? ie. I have the name="DepartmentsController" and I want to create from this the model "Department". Thanks, and keeeep on.

Snippets Manager replied on Mon, 2007/03/05 - 11:40am

updated. 10x.

Snippets Manager replied on Sat, 2006/12/30 - 11:43pm

This snippet would not work on case-sensitive filesystems, such as ones found on OSX or Linux. Line 1 should read: controllers ="#{RAILS_ROOT}/app/controllers").entries