mirror of https://github.com/rails/rails
Rely on Rails::Command's help output.
We end up with: ``` Usage: bin/rails routes [options] Options: -c, [--controller=CONTROLLER] # Filter by a specific controller, e.g. PostsController or Admin::PostsController. -g, [--grep=GREP] # Grep routes by a specific pattern. -E, [--expanded], [--no-expanded] # Print routes expanded vertically with parts explained. ``` which does miss the bit about routes being printed in order. Also: * Renames options to ease help output readability, then clarifies each option. * Fixes a bunch of indentation.
This commit is contained in:
parent
f6310a3f16
commit
6629d51a27
|
@ -81,18 +81,12 @@ module ActionDispatch
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_filter(filter)
|
||||
if filter[:controller]
|
||||
{ controller: /#{filter[:controller].downcase.sub(/_?controller\z/, '').sub('::', '/')}/ }
|
||||
elsif filter[:grep_pattern]
|
||||
{
|
||||
controller: /#{filter[:grep_pattern]}/,
|
||||
action: /#{filter[:grep_pattern]}/,
|
||||
verb: /#{filter[:grep_pattern]}/,
|
||||
name: /#{filter[:grep_pattern]}/,
|
||||
path: /#{filter[:grep_pattern]}/
|
||||
}
|
||||
elsif filter[:grep]
|
||||
{ controller: /#{filter[:grep]}/, action: /#{filter[:grep]}/,
|
||||
verb: /#{filter[:grep]}/, name: /#{filter[:grep]}/, path: /#{filter[:grep]}/ }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -153,17 +147,18 @@ module ActionDispatch
|
|||
|
||||
def no_routes(routes, filter)
|
||||
@buffer <<
|
||||
if routes.none?
|
||||
<<~MESSAGE
|
||||
You don't have any routes defined!
|
||||
if routes.none?
|
||||
<<~MESSAGE
|
||||
You don't have any routes defined!
|
||||
|
||||
Please add some routes in config/routes.rb.
|
||||
MESSAGE
|
||||
elsif filter.key?(:controller)
|
||||
"No routes were found for this controller."
|
||||
elsif filter.key?(:grep)
|
||||
"No routes were found for this grep pattern."
|
||||
end
|
||||
|
||||
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
|
||||
|
@ -219,11 +214,11 @@ module ActionDispatch
|
|||
def draw_expanded_section(routes)
|
||||
routes.map.each_with_index do |r, i|
|
||||
<<~MESSAGE.chomp
|
||||
#{route_header(index: i + 1)}
|
||||
Prefix | #{r[:name]}
|
||||
Verb | #{r[:verb]}
|
||||
URI | #{r[:path]}
|
||||
Controller#Action | #{r[:reqs]}
|
||||
#{route_header(index: i + 1)}
|
||||
Prefix | #{r[:name]}
|
||||
Verb | #{r[:verb]}
|
||||
URI | #{r[:path]}
|
||||
Controller#Action | #{r[:reqs]}
|
||||
MESSAGE
|
||||
end
|
||||
end
|
||||
|
@ -266,7 +261,7 @@ module ActionDispatch
|
|||
<a href="http://guides.rubyonrails.org/routing.html">Rails Routing from the Outside In</a>.
|
||||
</li>
|
||||
</ul>
|
||||
MESSAGE
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
def result
|
||||
|
|
|
@ -5,19 +5,9 @@ require "rails/command"
|
|||
module Rails
|
||||
module Command
|
||||
class RoutesCommand < Base # :nodoc:
|
||||
class_option :controller, aliases: "-c", type: :string, desc: "Specifies the controller."
|
||||
class_option :grep_pattern, aliases: "-g", type: :string, desc: "Specifies grep pattern."
|
||||
class_option :expanded_format, aliases: "--expanded", type: :string, desc: "Turn on expanded format mode."
|
||||
|
||||
no_commands do
|
||||
def help
|
||||
say "Usage: Print out all defined routes in match order, with names."
|
||||
say ""
|
||||
say "Target specific controller with -c option, or grep routes using -g option"
|
||||
say "Use expanded format with --expanded option"
|
||||
say ""
|
||||
end
|
||||
end
|
||||
class_option :controller, aliases: "-c", desc: "Filter by a specific controller, e.g. PostsController or Admin::PostsController."
|
||||
class_option :grep, aliases: "-g", desc: "Grep routes by a specific pattern."
|
||||
class_option :expanded, type: :boolean, aliases: "-E", desc: "Print routes expanded vertically with parts explained."
|
||||
|
||||
def perform(*)
|
||||
require_application_and_environment!
|
||||
|
@ -32,7 +22,7 @@ module Rails
|
|||
end
|
||||
|
||||
def formatter
|
||||
if options.key?("expanded_format")
|
||||
if options.key?("expanded")
|
||||
ActionDispatch::Routing::ConsoleFormatter::Expanded.new
|
||||
else
|
||||
ActionDispatch::Routing::ConsoleFormatter::Sheet.new
|
||||
|
@ -40,7 +30,7 @@ module Rails
|
|||
end
|
||||
|
||||
def routes_filter
|
||||
options.symbolize_keys.slice(:controller, :grep_pattern)
|
||||
options.symbolize_keys.slice(:controller, :grep)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -40,18 +40,18 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
|
|||
|
||||
output = run_routes_command(["-g", "show"])
|
||||
assert_equal <<~MESSAGE, output
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
cart GET /cart(.:format) cart#show
|
||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
||||
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
|
||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
cart GET /cart(.:format) cart#show
|
||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
||||
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
|
||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
||||
MESSAGE
|
||||
|
||||
output = run_routes_command(["-g", "POST"])
|
||||
assert_equal <<~MESSAGE, output
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
POST /cart(.:format) cart#create
|
||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
POST /cart(.:format) cart#create
|
||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
||||
MESSAGE
|
||||
|
||||
output = run_routes_command(["-g", "basketballs"])
|
||||
|
@ -61,10 +61,10 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
|
|||
|
||||
test "rails routes with controller search key" do
|
||||
app_file "config/routes.rb", <<-RUBY
|
||||
Rails.application.routes.draw do
|
||||
get '/cart', to: 'cart#show'
|
||||
get '/basketball', to: 'basketball#index'
|
||||
end
|
||||
Rails.application.routes.draw do
|
||||
get '/cart', to: 'cart#show'
|
||||
get '/basketball', to: 'basketball#index'
|
||||
end
|
||||
RUBY
|
||||
|
||||
output = run_routes_command(["-c", "cart"])
|
||||
|
@ -79,12 +79,13 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
|
|||
|
||||
test "rails routes with namespaced controller search key" do
|
||||
app_file "config/routes.rb", <<-RUBY
|
||||
Rails.application.routes.draw do
|
||||
namespace :admin do
|
||||
resource :post
|
||||
end
|
||||
Rails.application.routes.draw do
|
||||
namespace :admin do
|
||||
resource :post
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
expected_output = [" Prefix Verb URI Pattern Controller#Action",
|
||||
" new_admin_post GET /admin/post/new(.:format) admin/posts#new",
|
||||
"edit_admin_post GET /admin/post/edit(.:format) admin/posts#edit",
|
||||
|
@ -103,17 +104,17 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
|
|||
|
||||
test "rails routes displays message when no routes are defined" do
|
||||
app_file "config/routes.rb", <<-RUBY
|
||||
Rails.application.routes.draw do
|
||||
end
|
||||
Rails.application.routes.draw do
|
||||
end
|
||||
RUBY
|
||||
|
||||
assert_equal <<~MESSAGE, run_routes_command
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
||||
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
|
||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
||||
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
||||
Prefix Verb URI Pattern Controller#Action
|
||||
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
|
||||
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
|
||||
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
|
||||
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
|
||||
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
|
||||
MESSAGE
|
||||
end
|
||||
|
||||
|
@ -123,44 +124,44 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
|
|||
IO.console.winsize = [0, 27]
|
||||
|
||||
app_file "config/routes.rb", <<-RUBY
|
||||
Rails.application.routes.draw do
|
||||
get '/cart', to: 'cart#show'
|
||||
end
|
||||
Rails.application.routes.draw do
|
||||
get '/cart', to: 'cart#show'
|
||||
end
|
||||
RUBY
|
||||
|
||||
output = run_routes_command(["--expanded"])
|
||||
|
||||
assert_equal <<~MESSAGE, output
|
||||
--[ Route 1 ]--------------
|
||||
Prefix | cart
|
||||
Verb | GET
|
||||
URI | /cart(.:format)
|
||||
Controller#Action | cart#show
|
||||
--[ Route 2 ]--------------
|
||||
Prefix | rails_service_blob
|
||||
Verb | GET
|
||||
URI | /rails/active_storage/blobs/:signed_id/*filename(.:format)
|
||||
Controller#Action | active_storage/blobs#show
|
||||
--[ Route 3 ]--------------
|
||||
Prefix | rails_blob_representation
|
||||
Verb | GET
|
||||
URI | /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)
|
||||
Controller#Action | active_storage/representations#show
|
||||
--[ Route 4 ]--------------
|
||||
Prefix | rails_disk_service
|
||||
Verb | GET
|
||||
URI | /rails/active_storage/disk/:encoded_key/*filename(.:format)
|
||||
Controller#Action | active_storage/disk#show
|
||||
--[ Route 5 ]--------------
|
||||
Prefix | update_rails_disk_service
|
||||
Verb | PUT
|
||||
URI | /rails/active_storage/disk/:encoded_token(.:format)
|
||||
Controller#Action | active_storage/disk#update
|
||||
--[ Route 6 ]--------------
|
||||
Prefix | rails_direct_uploads
|
||||
Verb | POST
|
||||
URI | /rails/active_storage/direct_uploads(.:format)
|
||||
Controller#Action | active_storage/direct_uploads#create
|
||||
--[ Route 1 ]--------------
|
||||
Prefix | cart
|
||||
Verb | GET
|
||||
URI | /cart(.:format)
|
||||
Controller#Action | cart#show
|
||||
--[ Route 2 ]--------------
|
||||
Prefix | rails_service_blob
|
||||
Verb | GET
|
||||
URI | /rails/active_storage/blobs/:signed_id/*filename(.:format)
|
||||
Controller#Action | active_storage/blobs#show
|
||||
--[ Route 3 ]--------------
|
||||
Prefix | rails_blob_representation
|
||||
Verb | GET
|
||||
URI | /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)
|
||||
Controller#Action | active_storage/representations#show
|
||||
--[ Route 4 ]--------------
|
||||
Prefix | rails_disk_service
|
||||
Verb | GET
|
||||
URI | /rails/active_storage/disk/:encoded_key/*filename(.:format)
|
||||
Controller#Action | active_storage/disk#show
|
||||
--[ Route 5 ]--------------
|
||||
Prefix | update_rails_disk_service
|
||||
Verb | PUT
|
||||
URI | /rails/active_storage/disk/:encoded_token(.:format)
|
||||
Controller#Action | active_storage/disk#update
|
||||
--[ Route 6 ]--------------
|
||||
Prefix | rails_direct_uploads
|
||||
Verb | POST
|
||||
URI | /rails/active_storage/direct_uploads(.:format)
|
||||
Controller#Action | active_storage/direct_uploads#create
|
||||
MESSAGE
|
||||
ensure
|
||||
IO.console.winsize = previous_console_winsize
|
||||
|
|
Loading…
Reference in New Issue