From 2f6981216cba70251f16949905590df95c30d19c Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sat, 22 Sep 2012 18:59:42 -0700 Subject: [PATCH] Move and refactor show-routes --- lib/pry-rails/commands.rb | 56 ----------------------- lib/pry-rails/commands/show_models.rb | 3 +- lib/pry-rails/commands/show_routes.rb | 64 +++++++++++++++++++++++++++ spec/show_routes_spec.rb | 15 +++++++ 4 files changed, 81 insertions(+), 57 deletions(-) create mode 100644 lib/pry-rails/commands/show_routes.rb create mode 100644 spec/show_routes_spec.rb diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb index 9e28da2..b84ca72 100644 --- a/lib/pry-rails/commands.rb +++ b/lib/pry-rails/commands.rb @@ -1,61 +1,5 @@ module PryRails Commands = Pry::CommandSet.new do - create_command "show-routes", "Print out all defined routes in match order, with names." do - group "Rails" - - def options(opt) - opt.banner unindent <<-USAGE - Usage: show-routes [-G] - - show-routes displays the current Rails app's routes. - USAGE - - opt.on :G, "grep", "Filter output by regular expression", :argument => true - end - - def process - Rails.application.reload_routes! - all_routes = Rails.application.routes.routes - - all_routes = begin - begin - # rails 4 - require 'action_dispatch/routing/inspector' - inspector = ActionDispatch::Routing::RoutesInspector.new - rescue LoadError => e - # rails 3.2 - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new - end - inspector.format(all_routes) - rescue LoadError => e - # rails 3.0 and 3.1. cribbed from - # https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake - routes = all_routes.collect do |route| - - reqs = route.requirements.dup - reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ - reqs = reqs.empty? ? "" : reqs.inspect - - {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs} - end - - # Skip the route if it's internal info route - routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} } - - name_width = routes.map{ |r| r[:name].length }.max - verb_width = routes.map{ |r| r[:verb].length }.max - path_width = routes.map{ |r| r[:path].length }.max - - routes.map do |r| - "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}" - end - end - - output.puts all_routes.grep(Regexp.new(opts[:G] || ".")).join "\n" - end - end - create_command "show-middleware" do group "Rails" diff --git a/lib/pry-rails/commands/show_models.rb b/lib/pry-rails/commands/show_models.rb index 1312972..4175a56 100644 --- a/lib/pry-rails/commands/show_models.rb +++ b/lib/pry-rails/commands/show_models.rb @@ -1,7 +1,8 @@ # encoding: UTF-8 -PryRails::Commands.create_command "show-models", "Show all defined models." do +PryRails::Commands.create_command "show-models" do group "Rails" + description "Show all models." def options(opt) opt.banner unindent <<-USAGE diff --git a/lib/pry-rails/commands/show_routes.rb b/lib/pry-rails/commands/show_routes.rb new file mode 100644 index 0000000..04b96e5 --- /dev/null +++ b/lib/pry-rails/commands/show_routes.rb @@ -0,0 +1,64 @@ +# encoding: UTF-8 + +PryRails::Commands.create_command "show-routes" do + group "Rails" + description "Show all routes in match order." + + def options(opt) + opt.banner unindent <<-USAGE + Usage: show-routes [-G] + + show-routes displays the current Rails app's routes. + USAGE + + opt.on :G, "grep", "Filter output by regular expression", :argument => true + end + + def process + Rails.application.reload_routes! + all_routes = Rails.application.routes.routes + + formatted = case Rails.version + when /^4/ + process_rails_4(all_routes) + when /^3\.2/ + process_rails_3_2(all_routes) + else + process_rails_3_0_and_3_1(all_routes) + end + + output.puts formatted.grep(Regexp.new(opts[:G] || ".")).join("\n") + end + + # Cribbed from https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake + def process_rails_3_0_and_3_1(all_routes) + routes = all_routes.collect do |route| + reqs = route.requirements.dup + reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ + reqs = reqs.empty? ? "" : reqs.inspect + + {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs} + end + + # Skip the route if it's internal info route + routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} } + + name_width = routes.map{ |r| r[:name].length }.max + verb_width = routes.map{ |r| r[:verb].length }.max + path_width = routes.map{ |r| r[:path].length }.max + + routes.map do |r| + "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}" + end + end + + def process_rails_3_2(all_routes) + require 'rails/application/route_inspector' + Rails::Application::RouteInspector.new.format(all_routes) + end + + def process_rails_4(all_routes) + require 'action_dispatch/routing/inspector' + ActionDispatch::Routing::RoutesInspector.new.format(all_routes) + end +end diff --git a/spec/show_routes_spec.rb b/spec/show_routes_spec.rb new file mode 100644 index 0000000..e1b2000 --- /dev/null +++ b/spec/show_routes_spec.rb @@ -0,0 +1,15 @@ +# encoding: UTF-8 + +# We can just have a smoke test for this one since it's mostly using built-in +# Rails functionality. Plus the output is a bit different between Rails +# versions, so that's annoying. + +require 'spec_helper' + +describe "show-routes" do + it "should print a list of routes" do + output = mock_pry('show-routes', 'exit-all') + + output.must_match %r{^edit_pokemon GET /pokemon/edit} + end +end