views:

42

answers:

1

I am always forced to make my terminal window two dual monitors wide just to see read them right. I'm not a stickler for buttery GUI's, but this is bordering retarded.

Is there a pretty print for this command?

+1  A: 

I've rewritten the rake routes command slightly to generate a slightly more usable html version of the rake routes output

Put this is lib/tasks/pretty_routes.rake and call rake pretty_routes and it should be slightly better

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|
    name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s
    verb = route.conditions[:method].to_s.upcase
    segs = route.segments.inject("") { |str,s| str << s.to_s }
    segs.chop! if segs.length > 1
    reqs = route.requirements.empty? ? "" : route.requirements.inspect
    {:name => name, :verb => verb, :segs => segs, :reqs => reqs}
  end
  File.open(File.join(RAILS_ROOT, "routes.html"), "w") do |f|
    f.puts "<html><head><title>Rails Routes</title></head><body><table border=1>"
    f.puts "<tr><th>Name</th><th>Verb</th><th>Segments</th><th>Requirements</th></tr>"
    routes.each do |r|
      f.puts "<tr><td>#{r[:name]}</td><td>#{r[:verb]}</td><td>#{r[:segs]}</td><td>#{r[:reqs]}</td></tr>"
    end
    f.puts "</table></body></html>"
  end
  `open #{File.join(RAILS_ROOT, "routes.html")}`
end

The last line only works on mac os x, but it automatically opens the file in your browser. If you are on a different platform, you will have to change the command.

Ben Hughes
ITs clever. And I absolutely give you kudos for this, but I don't like getting all the routes at once. I usually go for a `rake routes | grep somethingSpecific`
Trip
This seems broken with rails3? rake aborted!undefined method `segments' for #<ActionDispatch::Routing::Route:0x000001058a9138>(See full trace by running task with --trace)
Lichtamberg
Lichtamberg: absolutely. I'm still on Rails 2.3.x, rails 3 entirely changed routing, so you will have to modify the solution to work for you.
Ben Hughes
Trip: you can always just use find in the browser to narrow it down. Its basically equivalent to grepping for something in the output of rake routes.
Ben Hughes
ah good point. one of these days i'll write something exclusively for the terminal window with pretty colors. thanks anyway
Trip