Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8795: rails_routes_task.diff

File rails_routes_task.diff, 1.2 kB (added by hasmanyjosh, 2 years ago)

rails:routes task

  • railties/lib/tasks/framework.rake

    old new  
    109109      FileUtils.cp(RAILTIES_PATH + '/environments/boot.rb', RAILS_ROOT + '/config/boot.rb') 
    110110    end 
    111111  end 
     112 
     113  desc 'Print out all defined routes in match order, with names.' 
     114  task :routes => :environment do 
     115    routes = ActionController::Routing::Routes.routes.collect do |route| 
     116      name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s 
     117      verb = route.conditions[:method].to_s.upcase 
     118      segs = route.segments.inject("") { |str,s| str << s.to_s } 
     119      reqs = route.requirements.empty? ? "" : route.requirements.inspect 
     120      {:name => name, :verb => verb, :segs => segs, :reqs => reqs} 
     121    end 
     122    name_width = routes.collect {|r| r[:name]}.collect {|n| n.length}.max 
     123    verb_width = routes.collect {|r| r[:verb]}.collect {|v| v.length}.max 
     124    segs_width = routes.collect {|r| r[:segs]}.collect {|s| s.length}.max 
     125    routes.each do |r| 
     126      puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:segs].ljust(segs_width)} #{r[:reqs]}" 
     127    end 
     128  end 
     129 
    112130end