Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make "rake routes" look better?

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

Is there a pretty print for this command?

like image 824
Trip Avatar asked Oct 21 '10 11:10

Trip


People also ask

What does rake routes do?

rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.

What is RESTful routes in Rails?

In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.


1 Answers

EDIT: The answer below was packaged into the html_routes gem which supports Rails 3 and 4.

The code below was made with the current Rails 3.2.3, groups by controller and looks awesome. Remember to change the <Your APP> to your app name and add gem 'syntax' to your Gemfile.

Sample image

desc 'Pretty print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.'

task :routes => :environment do
if ENV['CONTROLLER']
  all_routes = <Your APP>::Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] }
else
  all_routes = <Your APP>::Application.routes
end

convertor = Syntax::Convertors::HTML.for_syntax "ruby"

File.open(File.join(Rails.root, "routes.html"), "w") do |f|
  f.puts "<html><head><title>Your APP</title>
         <style type='text/css'>
         body { background-color: #333; color: #FFF; }
         table { border: 1px solid #777; background-color: #111; }
         td, th { font-size: 11pt; text-align: left; padding-right: 10px; }
         th { font-size: 12pt; }
         pre { maring: 0; padding: 0; }
         .contrl_head { font-size: 14pt; padding: 15px 0 5px 0; }
         .contrl_name { color: #ACE; }
         .punct { color: #99F; font-weight: bold; }
         .symbol { color: #7DD; }
         .regex { color: #F66; }
         .string { color: #F99; }4
         </style></head>
         <body>"

  last_contrl = nil

  routes = all_routes.routes.collect do |route|
    if !route.requirements.empty?
      if route.requirements[:controller] != last_contrl
        f.puts "</table>" if last_contrl
        last_contrl = route.requirements[:controller]
        f.puts "<div class='contrl_head'><b>Controller: <span class='contrl_name'>#{last_contrl}</span></b></div>" +
               "<table width='100%' border='0'><tr><th>Name</th><th>Verb</th><th>Path</th><th>Requirements</th></tr>" 
      end

      reqs = route.requirements.inspect
      verb = route.verb.source
      verb = verb[1..(verb.length-2)] if verb
      r = { :name => route.name, :verb => verb, :path => route.path, :reqs => reqs }
      f.puts "<tr><td width='12%'><b>#{r[:name]}</b></td><td width='5%'><b>#{r[:verb]}</b></td>" +
              "<td width='3%'>#{r[:path]}</td><td width='80%'>#{convertor.convert(r[:reqs])}</td></tr>"
    end
  end

  f.puts "</table></body></html>"
end
end
like image 184
Pierre Pretorius Avatar answered Oct 11 '22 19:10

Pierre Pretorius