Skip to content

Instantly share code, notes, and snippets.

@tysone
Created December 30, 2011 17:04
Show Gist options
  • Save tysone/1540614 to your computer and use it in GitHub Desktop.
Save tysone/1540614 to your computer and use it in GitHub Desktop.
desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'
task :pretty_routes => :environment do
all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes
routes = all_routes.collect do |route|
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => route.name, :verb => route.verb, :path => route.path, :reqs => reqs}
end
File.open(File.join(RAILS_ROOT, "routes.html"), "w") do |f|
f.puts "<html><head><title>Rails 3 Routes</title></head><body><table border=1>"
f.puts "<tr><th>Name</th><th>Verb</th><th>Path</th><th>Requirements</th></tr>"
routes.each do |r|
f.puts "<tr><td>#{r[:name]}</td><td>#{r[:verb]}</td><td>#{r[:path]}</td><td>#{r[:reqs]}</td></tr>"
end
f.puts "</table></body></html>"
end
`open #{File.join(RAILS_ROOT, "routes.html")}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment