Last active
December 15, 2015 19:49
-
-
Save sstelfox/5314342 to your computer and use it in GitHub Desktop.
This is my version of the Rails rake routes task for Sinatra. I've found this to be endlessly useful in debugging route ordering.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra_app' | |
# This comes from the sinatra-contrib gem | |
require 'sinatra/decompile' | |
# This will print all of the configured routes for the sinatra application in | |
# the order that the route was first specified. All of the methods will be | |
# combined together and it will format the output into neat columns. Very useful | |
# for debugging when an incorrect route is being hit. | |
desc "List all of the routes configured" | |
task :routes => [:environment] do | |
rl = Default::App.routes.each_with_object(Hash.new([])) do |verb, route_listing| | |
verb[1].each do |route| | |
route_listing[route[0].source] += [verb[0]] | |
end | |
end | |
longest_column = rl.values.map { |v| v.join(", ").length }.max | |
rl.each_pair do |route, methods| | |
printf("%-#{longest_column}s %s\n", methods.sort.join(", "), Sinatra::Decompile.decompile(route)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment