diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2016-02-01 22:09:22 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2016-02-01 22:09:22 +0100 |
commit | baae952588395a1bc04122b00a94baa5af8cdf35 (patch) | |
tree | 5caf6067ffc8c3ae5e4644a595d118f00a704e12 /actionpack/lib/action_dispatch | |
parent | 0cbcae59dd402ff27f6dbf76659b67a77776fe37 (diff) | |
parent | 8a436fdd98c63cc0a7a6d2c642c18d33421dc6ad (diff) | |
download | rails-baae952588395a1bc04122b00a94baa5af8cdf35.tar.gz rails-baae952588395a1bc04122b00a94baa5af8cdf35.tar.bz2 rails-baae952588395a1bc04122b00a94baa5af8cdf35.zip |
Merge pull request #23225 from vipulnsward/20420-rake-routes-options
Add options for rake routes task
Diffstat (limited to 'actionpack/lib/action_dispatch')
-rw-r--r-- | actionpack/lib/action_dispatch/routing.rb | 3 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/inspector.rb | 34 |
2 files changed, 21 insertions, 16 deletions
diff --git a/actionpack/lib/action_dispatch/routing.rb b/actionpack/lib/action_dispatch/routing.rb index d00b2c3eb5..6cde5b2900 100644 --- a/actionpack/lib/action_dispatch/routing.rb +++ b/actionpack/lib/action_dispatch/routing.rb @@ -239,7 +239,8 @@ module ActionDispatch # # rails routes # - # Target specific controllers by prefixing the command with <tt>CONTROLLER=x</tt>. + # Target specific controllers by prefixing the command with <tt>--controller</tt> option + # - or its <tt>-c</tt> shorthand. # module Routing extend ActiveSupport::Autoload diff --git a/actionpack/lib/action_dispatch/routing/inspector.rb b/actionpack/lib/action_dispatch/routing/inspector.rb index 69e6dd5215..1ca2a3b683 100644 --- a/actionpack/lib/action_dispatch/routing/inspector.rb +++ b/actionpack/lib/action_dispatch/routing/inspector.rb @@ -60,12 +60,11 @@ module ActionDispatch end def format(formatter, filter = nil) - routes_to_display = filter_routes(filter) - + filter_options = normalize_filter(filter) + routes_to_display = filter_routes(filter_options) routes = collect_routes(routes_to_display) - if routes.none? - formatter.no_routes(collect_routes(@routes), filter) + formatter.no_routes(collect_routes(@routes)) return formatter.result end @@ -82,10 +81,21 @@ module ActionDispatch private - def filter_routes(filter) - if filter - filter_name = filter.underscore.sub(/_controller$/, '') - @routes.select { |route| route.defaults[:controller] == filter_name } + def normalize_filter(filter) + if filter.is_a?(Hash) && filter[:controller] + {controller: /#{filter[:controller].downcase.sub(/_?controller\z/, '').sub('::', '/')}/} + elsif filter.is_a?(String) + {controller: /#{filter}/, action: /#{filter}/} + else + nil + end + end + + def filter_routes(filter_options) + if filter_options + @routes.select do |route| + filter_options.any? { |default, filter| route.defaults[default] =~ filter } + end else @routes end @@ -137,7 +147,7 @@ module ActionDispatch @buffer << draw_header(routes) end - def no_routes(routes, filter) + def no_routes(routes) @buffer << if routes.none? <<-MESSAGE.strip_heredoc @@ -145,8 +155,6 @@ module ActionDispatch Please add some routes in config/routes.rb. MESSAGE - elsif missing_controller?(filter) - "The controller #{filter} does not exist!" else "No routes were found for this controller" end @@ -154,10 +162,6 @@ module ActionDispatch end private - def missing_controller?(controller_name) - [ controller_name.camelize, "#{controller_name.camelize}Controller" ].none?(&:safe_constantize) - end - def draw_section(routes) header_lengths = ['Prefix', 'Verb', 'URI Pattern'].map(&:length) name_width, verb_width, path_width = widths(routes).zip(header_lengths).map(&:max) |