Skip to content

Instantly share code, notes, and snippets.

@saten
Created August 17, 2012 09:29
Show Gist options
  • Save saten/3377405 to your computer and use it in GitHub Desktop.
Save saten/3377405 to your computer and use it in GitHub Desktop.
Enumerates routed controllers and actions and checks if they exist in the code (simple and quick, without corner cases). Tested on Rails 3.2.8
Rails.application.eager_load!
all_routes = Rails.application.routes.routes
controllers=ApplicationController.descendants
#controller_names = controllers.collect {|c| c.to_s.underscore.sub('_controller','') }
controller_names={}
controllers.each {|c|
controller_names[c.to_s]=c.to_s.underscore.sub('_controller','')
}.size
missing_stuff_names = {}
all_routes.each {|route|
routed_controller = route.defaults[:controller]
routed_action = route.defaults[:action]
next unless routed_controller
#puts "routed_controller: #{routed_controller} , routed_action: #{routed_action}"
unless controller_names.values.include? routed_controller
#controller is routed but totally missing from code
missing_stuff_names[routed_controller]||=[]
missing_stuff_names[routed_controller] << routed_action
pp "#{routed_controller} totally missing, should also have action: #{routed_action}"
else
unless controller_names.key(routed_controller).constantize.action_methods.to_a.include? routed_action
#controller is present in code but misses some routed actions
missing_stuff_names[routed_controller]||=[]
missing_stuff_names[routed_controller] << routed_action
pp "#{routed_controller} missing action: #{routed_action}"
end
end
}.size
pp missing_stuff_names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment