Never been to DZone Snippets before?

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

Generate a list of Rails controllers and methods (See related posts)

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 on this post

Qaexl posts on Nov 28, 2007 at 00:37
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
rubp posts on Nov 30, 2007 at 11:25
updated. 10x.
csbartus posts on Dec 11, 2007 at 13:35
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.

statikuz posts on Feb 06, 2008 at 19:05
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)

You need to create an account or log in to post comments to this site.


Click here to browse all 4834 code snippets

Related Posts