Class: PryRails::ShowRoutes
- Inherits:
-
Pry::ClassCommand
- Object
- Pry::ClassCommand
- PryRails::ShowRoutes
- Defined in:
- lib/pry-rails/commands/show_routes.rb
Instance Method Summary collapse
-
#grep_routes(formatted) ⇒ Object
Takes an array of lines.
- #options(opt) ⇒ Object
- #process ⇒ Object
- #process_rails_3_0_and_3_1(all_routes) ⇒ Object
- #process_rails_3_2(all_routes) ⇒ Object
- #process_rails_4_and_5(all_routes) ⇒ Object
Instance Method Details
#grep_routes(formatted) ⇒ Object
Takes an array of lines. Returns a list filtered by the conditions in ‘opts`.
34 35 36 37 38 39 40 41 |
# File 'lib/pry-rails/commands/show_routes.rb', line 34 def grep_routes(formatted) return formatted unless opts[:G] grep_opts = opts[:G] grep_opts.reduce(formatted) do |lines, pattern| lines.grep(Regexp.new(pattern)) end end |
#options(opt) ⇒ Object
10 11 12 13 14 |
# File 'lib/pry-rails/commands/show_routes.rb', line 10 def (opt) opt.on :G, "grep", "Filter output by regular expression", :argument => true, :as => Array end |
#process ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pry-rails/commands/show_routes.rb', line 16 def process Rails.application.reload_routes! all_routes = Rails.application.routes.routes formatted = case Rails.version.to_s when /^[45]/ process_rails_4_and_5(all_routes) when /^3\.2/ process_rails_3_2(all_routes) else process_rails_3_0_and_3_1(all_routes) end output.puts grep_routes(formatted).join("\n") end |
#process_rails_3_0_and_3_1(all_routes) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pry-rails/commands/show_routes.rb', line 44 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 |
#process_rails_3_2(all_routes) ⇒ Object
65 66 67 68 |
# File 'lib/pry-rails/commands/show_routes.rb', line 65 def process_rails_3_2(all_routes) require 'rails/application/route_inspector' Rails::Application::RouteInspector.new.format(all_routes) end |
#process_rails_4_and_5(all_routes) ⇒ Object
70 71 72 73 |
# File 'lib/pry-rails/commands/show_routes.rb', line 70 def process_rails_4_and_5(all_routes) require 'action_dispatch/routing/inspector' ActionDispatch::Routing::RoutesInspector.new(all_routes).format(ActionDispatch::Routing::ConsoleFormatter.new).split(/\n/) end |