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

11.17.2007
| 19364 views |
  • 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 = 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


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 -  
       ApplicationController.new.methods).sort.each {|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}\",
                                                  :controller=>\"#{name_short}\",
                                                  :action=>\"#{met}\")"}
  end
  end

    

Comments

Carla Brian replied on Sun, 2012/07/22 - 12:06am

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 - 1: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 - 11: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 - 7:36am

this one works with rake: controller.camelize.gsub(".rb","").constantize.new.class.action_methods.each { |action|

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

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

Francisco Viramontes replied on Tue, 2008/07/01 - 9: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 = Dir.new("#{RAILS_ROOT}/app/controllers").entries controllers.each do |controller| if controller =~ /_controller/ controller.camelize.gsub(".rb","").constantize.new.action_method_names.each { |action| if Right.find_all_by_controller_name_and_controller_action(controller.camelize.gsub(".rb","").constantize.controller_path, action).empty? Right.new(:controller_name => controller.camelize.gsub(".rb","").constantize.controller_path, :controller_action => action).save end } end end end

Snippets Manager replied on Wed, 2008/02/06 - 7: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| controller.new.action_method_names.each { |action| if find_all_by_controller_and_action(controller.controller_path, action).empty? self.new(:controller => 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 - 2: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 - 12:40pm

updated. 10x.

Snippets Manager replied on Sun, 2006/12/31 - 12:43am

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