diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2018-03-11 20:48:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-11 20:48:50 +0100 |
commit | ee01c753a3f6fc459e4215117a92085f47f46833 (patch) | |
tree | 484a95277de3dcaa7f3c5f6c65aa728d78e6c323 /actionpack | |
parent | 58eda3cfd8b670718ba9b99bdbe137c4320ec32f (diff) | |
parent | 012a4a3842db66b5fc6b190cb61d4d845d1cd527 (diff) | |
download | rails-ee01c753a3f6fc459e4215117a92085f47f46833.tar.gz rails-ee01c753a3f6fc459e4215117a92085f47f46833.tar.bz2 rails-ee01c753a3f6fc459e4215117a92085f47f46833.zip |
Merge pull request #32160 from bogdanvlviv/improve-rails-routes-expanded
Draw line of a route name to the end of row console on `rails routes --expanded`
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/inspector.rb | 11 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing/inspector_test.rb | 15 |
2 files changed, 20 insertions, 6 deletions
diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index de200fada0..19dfb44283 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "delegate" +require "io/console/size" module ActionDispatch module Routing @@ -225,7 +226,7 @@ module ActionDispatch def draw_expanded_section(routes) routes.map.each_with_index do |r, i| <<~MESSAGE - --[ Route #{i + 1} ]#{'-' * 60} + #{route_header(index: i + 1)} Prefix | #{r[:name]} Verb | #{r[:verb]} URI | #{r[:path]} @@ -233,6 +234,14 @@ module ActionDispatch MESSAGE end end + + def route_header(index:) + console_width = IO.console_size.second + header_prefix = "--[ Route #{index} ]" + dash_remainder = [console_width - header_prefix.size, 0].max + + "#{header_prefix}#{'-' * dash_remainder}" + end end end diff --git a/actionpack/test/dispatch/routing/inspector_test.rb b/actionpack/test/dispatch/routing/inspector_test.rb index 127212b228..43407724aa 100644 --- a/actionpack/test/dispatch/routing/inspector_test.rb +++ b/actionpack/test/dispatch/routing/inspector_test.rb @@ -3,6 +3,7 @@ require "abstract_unit" require "rails/engine" require "action_dispatch/routing/inspector" +require "io/console/size" class MountedRackApp def self.call(env) @@ -322,6 +323,9 @@ module ActionDispatch end def test_routes_when_expanded + previous_console_winsize = IO.console.winsize + IO.console.winsize = [0, 23] + engine = Class.new(Rails::Engine) do def self.inspect "Blog::Engine" @@ -337,31 +341,32 @@ module ActionDispatch mount engine => "/blog", :as => "blog" end - assert_equal ["--[ Route 1 ]------------------------------------------------------------", + assert_equal ["--[ Route 1 ]----------", "Prefix | custom_assets", "Verb | GET", "URI | /custom/assets(.:format)", "Controller#Action | custom_assets#show", - "--[ Route 2 ]------------------------------------------------------------", + "--[ Route 2 ]----------", "Prefix | custom_furnitures", "Verb | GET", "URI | /custom/furnitures(.:format)", "Controller#Action | custom_furnitures#show", - "--[ Route 3 ]------------------------------------------------------------", + "--[ Route 3 ]----------", "Prefix | blog", "Verb | ", "URI | /blog", "Controller#Action | Blog::Engine", "", "[ Routes for Blog::Engine ]", - "--[ Route 1 ]------------------------------------------------------------", + "--[ Route 1 ]----------", "Prefix | cart", "Verb | GET", "URI | /cart(.:format)", "Controller#Action | cart#show"], output + ensure + IO.console.winsize = previous_console_winsize end - def test_no_routes_matched_filter_when_expanded output = draw("rails/dummy", ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/ |