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
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






Comments
Carla Brian replied on Sun, 2012/07/22 - 12:06am
Snippets Manager replied on Mon, 2009/06/01 - 1:00pm
Alexander Danilenko replied on Tue, 2009/04/28 - 11:24am
@controller_actions = ActionController::Routing::Routes.routes.inject({}) do |controller_actions, route| (controller_actions[route.requirements[:controller]] ||= []) << route.requirements[:action] controller_actions endSnippets Manager replied on Fri, 2009/02/13 - 7:36am
Snippets Manager replied on Sun, 2009/01/25 - 11:07pm
Francisco Viramontes replied on Tue, 2008/07/01 - 9:23pm
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 endSnippets Manager replied on Wed, 2008/02/06 - 7:53pm
# 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 } endNote:action_method_namesjust callsaction_methodsbut that's a private method so you have to do something like put it in yourapplication.rbfile. (worked for me in a pinch)Snippets Manager replied on Tue, 2007/12/11 - 2:12pm
Snippets Manager replied on Mon, 2007/03/05 - 12:40pm
Snippets Manager replied on Sun, 2006/12/31 - 12:43am