aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/inspector.rb
diff options
context:
space:
mode:
authorbogdanvlviv <bogdanvlviv@gmail.com>2018-03-05 20:21:27 +0200
committerbogdanvlviv <bogdanvlviv@gmail.com>2018-03-13 11:58:52 +0200
commit304906f1bd4cd6f2831b6ae39a9ae54599d85569 (patch)
treee3c42dac0b1677b5aa88eaa733ff2d6b38bccf7b /actionpack/lib/action_dispatch/routing/inspector.rb
parente78b1e5e615978a221b68530641101451ab83e68 (diff)
downloadrails-304906f1bd4cd6f2831b6ae39a9ae54599d85569.tar.gz
rails-304906f1bd4cd6f2831b6ae39a9ae54599d85569.tar.bz2
rails-304906f1bd4cd6f2831b6ae39a9ae54599d85569.zip
Introduce `ActionDispatch::Routing::ConsoleFormatter::Base`
- Create `Base` and inherit `Sheet` and `Expanded` in order to - prevent code duplication. - Remove trailing "\n" for components of `Expanded`. - There is no need for `Expanded#header` to return `@buffer` so return `nil` instead. - Change `no_routes` message "No routes were found for this controller" since if use `-g`, it sounds incorrect. - Display `No routes were found for this controller.` if apply `-c`. - Display `No routes were found for this grep pattern.` if apply `-g`. Related to #32130
Diffstat (limited to 'actionpack/lib/action_dispatch/routing/inspector.rb')
-rw-r--r--actionpack/lib/action_dispatch/routing/inspector.rb75
1 files changed, 34 insertions, 41 deletions
diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb
index 19dfb44283..7656c263a3 100644
--- a/actionpack/lib/action_dispatch/routing/inspector.rb
+++ b/actionpack/lib/action_dispatch/routing/inspector.rb
@@ -61,11 +61,11 @@ module ActionDispatch
@routes = routes
end
- def format(formatter, filter = nil)
+ def format(formatter, filter = {})
routes_to_display = filter_routes(normalize_filter(filter))
routes = collect_routes(routes_to_display)
if routes.none?
- formatter.no_routes(collect_routes(@routes))
+ formatter.no_routes(collect_routes(@routes), filter)
return formatter.result
end
@@ -83,10 +83,16 @@ module ActionDispatch
private
def normalize_filter(filter)
- if filter.is_a?(Hash) && filter[:controller]
+ if filter[:controller]
{ controller: /#{filter[:controller].downcase.sub(/_?controller\z/, '').sub('::', '/')}/ }
- elsif filter
- { controller: /#{filter}/, action: /#{filter}/, verb: /#{filter}/, name: /#{filter}/, path: /#{filter}/ }
+ elsif filter[:grep_pattern]
+ {
+ controller: /#{filter[:grep_pattern]}/,
+ action: /#{filter[:grep_pattern]}/,
+ verb: /#{filter[:grep_pattern]}/,
+ name: /#{filter[:grep_pattern]}/,
+ path: /#{filter[:grep_pattern]}/
+ }
end
end
@@ -127,7 +133,7 @@ module ActionDispatch
end
module ConsoleFormatter
- class Sheet
+ class Base
def initialize
@buffer = []
end
@@ -137,18 +143,15 @@ module ActionDispatch
end
def section_title(title)
- @buffer << "\n#{title}:"
end
def section(routes)
- @buffer << draw_section(routes)
end
def header(routes)
- @buffer << draw_header(routes)
end
- def no_routes(routes)
+ def no_routes(routes, filter)
@buffer <<
if routes.none?
<<~MESSAGE
@@ -156,11 +159,27 @@ module ActionDispatch
Please add some routes in config/routes.rb.
MESSAGE
- else
- "No routes were found for this controller"
+ elsif filter.has_key?(:controller)
+ "No routes were found for this controller."
+ elsif filter.has_key?(:grep_pattern)
+ "No routes were found for this grep pattern."
end
@buffer << "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
end
+ end
+
+ class Sheet < Base
+ def section_title(title)
+ @buffer << "\n#{title}:"
+ end
+
+ def section(routes)
+ @buffer << draw_section(routes)
+ end
+
+ def header(routes)
+ @buffer << draw_header(routes)
+ end
private
@@ -186,46 +205,20 @@ module ActionDispatch
end
end
- class Expanded
- def initialize
- @buffer = []
- end
-
- def result
- @buffer.join("")
- end
-
+ class Expanded < Base
def section_title(title)
- @buffer << "\n#{"[ #{title} ]"}\n"
+ @buffer << "\n#{"[ #{title} ]"}"
end
def section(routes)
@buffer << draw_expanded_section(routes)
end
- def header(routes)
- @buffer
- end
-
- def no_routes(routes)
- @buffer <<
- if routes.none?
- <<~MESSAGE
- You don't have any routes defined!
-
- Please add some routes in config/routes.rb.\n
- MESSAGE
- else
- "No routes were found for this controller\n"
- end
- @buffer << "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
- end
-
private
def draw_expanded_section(routes)
routes.map.each_with_index do |r, i|
- <<~MESSAGE
+ <<~MESSAGE.chomp
#{route_header(index: i + 1)}
Prefix | #{r[:name]}
Verb | #{r[:verb]}