mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
e78b1e5e61
commit
304906f1bd
3 changed files with 54 additions and 67 deletions
|
@ -61,11 +61,11 @@ module ActionDispatch
|
||||||
@routes = routes
|
@routes = routes
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(formatter, filter = nil)
|
def format(formatter, filter = {})
|
||||||
routes_to_display = filter_routes(normalize_filter(filter))
|
routes_to_display = filter_routes(normalize_filter(filter))
|
||||||
routes = collect_routes(routes_to_display)
|
routes = collect_routes(routes_to_display)
|
||||||
if routes.none?
|
if routes.none?
|
||||||
formatter.no_routes(collect_routes(@routes))
|
formatter.no_routes(collect_routes(@routes), filter)
|
||||||
return formatter.result
|
return formatter.result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -83,10 +83,16 @@ module ActionDispatch
|
||||||
private
|
private
|
||||||
|
|
||||||
def normalize_filter(filter)
|
def normalize_filter(filter)
|
||||||
if filter.is_a?(Hash) && filter[:controller]
|
if filter[:controller]
|
||||||
{ controller: /#{filter[:controller].downcase.sub(/_?controller\z/, '').sub('::', '/')}/ }
|
{ controller: /#{filter[:controller].downcase.sub(/_?controller\z/, '').sub('::', '/')}/ }
|
||||||
elsif filter
|
elsif filter[:grep_pattern]
|
||||||
{ controller: /#{filter}/, action: /#{filter}/, verb: /#{filter}/, name: /#{filter}/, path: /#{filter}/ }
|
{
|
||||||
|
controller: /#{filter[:grep_pattern]}/,
|
||||||
|
action: /#{filter[:grep_pattern]}/,
|
||||||
|
verb: /#{filter[:grep_pattern]}/,
|
||||||
|
name: /#{filter[:grep_pattern]}/,
|
||||||
|
path: /#{filter[:grep_pattern]}/
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,7 +133,7 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
module ConsoleFormatter
|
module ConsoleFormatter
|
||||||
class Sheet
|
class Base
|
||||||
def initialize
|
def initialize
|
||||||
@buffer = []
|
@buffer = []
|
||||||
end
|
end
|
||||||
|
@ -136,6 +142,33 @@ module ActionDispatch
|
||||||
@buffer.join("\n")
|
@buffer.join("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def section_title(title)
|
||||||
|
end
|
||||||
|
|
||||||
|
def section(routes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def header(routes)
|
||||||
|
end
|
||||||
|
|
||||||
|
def no_routes(routes, filter)
|
||||||
|
@buffer <<
|
||||||
|
if routes.none?
|
||||||
|
<<~MESSAGE
|
||||||
|
You don't have any routes defined!
|
||||||
|
|
||||||
|
Please add some routes in config/routes.rb.
|
||||||
|
MESSAGE
|
||||||
|
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)
|
def section_title(title)
|
||||||
@buffer << "\n#{title}:"
|
@buffer << "\n#{title}:"
|
||||||
end
|
end
|
||||||
|
@ -148,20 +181,6 @@ module ActionDispatch
|
||||||
@buffer << draw_header(routes)
|
@buffer << draw_header(routes)
|
||||||
end
|
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.
|
|
||||||
MESSAGE
|
|
||||||
else
|
|
||||||
"No routes were found for this controller"
|
|
||||||
end
|
|
||||||
@buffer << "For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def draw_section(routes)
|
def draw_section(routes)
|
||||||
|
@ -186,46 +205,20 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Expanded
|
class Expanded < Base
|
||||||
def initialize
|
|
||||||
@buffer = []
|
|
||||||
end
|
|
||||||
|
|
||||||
def result
|
|
||||||
@buffer.join("")
|
|
||||||
end
|
|
||||||
|
|
||||||
def section_title(title)
|
def section_title(title)
|
||||||
@buffer << "\n#{"[ #{title} ]"}\n"
|
@buffer << "\n#{"[ #{title} ]"}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def section(routes)
|
def section(routes)
|
||||||
@buffer << draw_expanded_section(routes)
|
@buffer << draw_expanded_section(routes)
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def draw_expanded_section(routes)
|
def draw_expanded_section(routes)
|
||||||
routes.map.each_with_index do |r, i|
|
routes.map.each_with_index do |r, i|
|
||||||
<<~MESSAGE
|
<<~MESSAGE.chomp
|
||||||
#{route_header(index: i + 1)}
|
#{route_header(index: i + 1)}
|
||||||
Prefix | #{r[:name]}
|
Prefix | #{r[:name]}
|
||||||
Verb | #{r[:verb]}
|
Verb | #{r[:verb]}
|
||||||
|
|
|
@ -20,7 +20,7 @@ module ActionDispatch
|
||||||
@set = ActionDispatch::Routing::RouteSet.new
|
@set = ActionDispatch::Routing::RouteSet.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw(options = nil, formater = ActionDispatch::Routing::ConsoleFormatter::Sheet.new, &block)
|
def draw(options = {}, formater = ActionDispatch::Routing::ConsoleFormatter::Sheet.new, &block)
|
||||||
@set.draw(&block)
|
@set.draw(&block)
|
||||||
inspector = ActionDispatch::Routing::RoutesInspector.new(@set.routes)
|
inspector = ActionDispatch::Routing::RoutesInspector.new(@set.routes)
|
||||||
inspector.format(formater, options).split("\n")
|
inspector.format(formater, options).split("\n")
|
||||||
|
@ -306,7 +306,7 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_routes_can_be_filtered
|
def test_routes_can_be_filtered
|
||||||
output = draw("posts") do
|
output = draw(grep_pattern: "posts") do
|
||||||
resources :articles
|
resources :articles
|
||||||
resources :posts
|
resources :posts
|
||||||
end
|
end
|
||||||
|
@ -335,7 +335,7 @@ module ActionDispatch
|
||||||
get "/cart", to: "cart#show"
|
get "/cart", to: "cart#show"
|
||||||
end
|
end
|
||||||
|
|
||||||
output = draw(nil, ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do
|
output = draw({}, ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do
|
||||||
get "/custom/assets", to: "custom_assets#show"
|
get "/custom/assets", to: "custom_assets#show"
|
||||||
get "/custom/furnitures", to: "custom_furnitures#show"
|
get "/custom/furnitures", to: "custom_furnitures#show"
|
||||||
mount engine => "/blog", :as => "blog"
|
mount engine => "/blog", :as => "blog"
|
||||||
|
@ -368,18 +368,18 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_routes_matched_filter_when_expanded
|
def test_no_routes_matched_filter_when_expanded
|
||||||
output = draw("rails/dummy", ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do
|
output = draw({ grep_pattern: "rails/dummy" }, ActionDispatch::Routing::ConsoleFormatter::Expanded.new) do
|
||||||
get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
|
get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal [
|
assert_equal [
|
||||||
"No routes were found for this controller",
|
"No routes were found for this grep pattern.",
|
||||||
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
||||||
], output
|
], output
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_not_routes_when_expanded
|
def test_not_routes_when_expanded
|
||||||
output = draw("rails/dummy", ActionDispatch::Routing::ConsoleFormatter::Expanded.new) {}
|
output = draw({ grep_pattern: "rails/dummy" }, ActionDispatch::Routing::ConsoleFormatter::Expanded.new) {}
|
||||||
|
|
||||||
assert_equal [
|
assert_equal [
|
||||||
"You don't have any routes defined!",
|
"You don't have any routes defined!",
|
||||||
|
@ -391,7 +391,7 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_routes_can_be_filtered_with_namespaced_controllers
|
def test_routes_can_be_filtered_with_namespaced_controllers
|
||||||
output = draw("admin/posts") do
|
output = draw(grep_pattern: "admin/posts") do
|
||||||
resources :articles
|
resources :articles
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
resources :posts
|
resources :posts
|
||||||
|
@ -439,24 +439,24 @@ module ActionDispatch
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal [
|
assert_equal [
|
||||||
"No routes were found for this controller",
|
"No routes were found for this controller.",
|
||||||
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
||||||
], output
|
], output
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_routes_matched_filter
|
def test_no_routes_matched_filter
|
||||||
output = draw("rails/dummy") do
|
output = draw(grep_pattern: "rails/dummy") do
|
||||||
get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
|
get "photos/:id" => "photos#show", :id => /[A-Z]\d{5}/
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal [
|
assert_equal [
|
||||||
"No routes were found for this controller",
|
"No routes were found for this grep pattern.",
|
||||||
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
"For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html."
|
||||||
], output
|
], output
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_no_routes_were_defined
|
def test_no_routes_were_defined
|
||||||
output = draw("Rails::DummyController") {}
|
output = draw(grep_pattern: "Rails::DummyController") {}
|
||||||
|
|
||||||
assert_equal [
|
assert_equal [
|
||||||
"You don't have any routes defined!",
|
"You don't have any routes defined!",
|
||||||
|
|
|
@ -36,13 +36,7 @@ module Rails
|
||||||
private
|
private
|
||||||
|
|
||||||
def routes_filter
|
def routes_filter
|
||||||
if options.has_key?("controller")
|
options.symbolize_keys.slice(:controller, :grep_pattern)
|
||||||
{ controller: options["controller"] }
|
|
||||||
elsif options.has_key?("grep_pattern")
|
|
||||||
options["grep_pattern"]
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue