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

About this user

john www.bioldata.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Generate a list of Rails controllers and methods

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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS