代码之家  ›  专栏  ›  技术社区  ›  Trip

有没有办法让“rake routes”看起来更好?

  •  16
  • Trip  · 技术社区  · 14 年前

    我总是被迫让我的终端窗口两个双显示器宽只是为了看他们读对。我不喜欢巴特·圭,但这太荒谬了。

    这个命令有漂亮的指纹吗?

    8 回复  |  直到 5 年前
        1
  •  10
  •   Pierre Pretorius    10 年前

    编辑:下面的答案被打包到 html_routes 支持Rails 3和Rails 4的gem。

    钢轨3.2.3 ,按控制器分组,看起来棒极了。记住要改变 <Your APP> 将gem'syntax'添加到gem文件中。

    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
    
        2
  •  9
  •   huug    12 年前
        3
  •  5
  •   Vibhuti    12 年前
    Rails 3.1 version, Replace all <YourApp> tag with your application name.
    
    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'] ? <YourApp>::Application.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : <YourApp>::Application.routes
      routes = all_routes.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
    end
    
        4
  •  4
  •   ic3b3rg    12 年前

    我稍微重写了rake routes命令,以生成一个稍微有用的rake routes输出的html版本

    pretty_routes.rake 把这个放进去 lib/tasks/ 打电话来 rake pretty_routes 应该会好一点

    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
    

    倒数第二行只适用于Rails2.x中的MacOSX an,但它会在浏览器中自动打开文件。如果您在不同的平台上,则必须更改命令。

     `open #{File.join(Rails.root, "routes.html")}`
    
        5
  •  3
  •   Ivan -Oats- Storck    11 年前

    您可以使用六分仪在浏览器中打印路由: https://github.com/schneems/sextant

        6
  •  2
  •   Community chadoh    7 年前

    对于Rails 3,您可以使用: Rails.application.routes.routes.to_a (见 my original answer )

        7
  •  2
  •   BenKoshy Mayinx    5 年前

    使用rails提供的内置显示(单击下面的链接):

    1. 启动服务器 rails s
    2. : http://localhost:3000/rails/info/routes
    3. 这就是它的样子-单击图片放大:

    This is how it looks like!

        8
  •  1
  •   lzap    13 年前

    我准备了工作版本 . 享受吧。

    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
    end