From 1a830cbd830c7f80936dff7e3c8b26f60dcc371d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 11 Aug 2015 14:44:17 -0700 Subject: split paths by type this simplifies the "downstream" logic since we know we'll only be dealing with one particular type --- actionpack/lib/action_dispatch/routing/mapper.rb | 43 +++++++++++++++--------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index e508b12350..6b77912ed2 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1538,43 +1538,54 @@ module ActionDispatch end controller = options.delete(:controller) || @scope[:controller] + option_path = options.delete :path - paths.each do |_path| + path_types = paths.group_by(&:class) + path_types.fetch(String, []).each do |_path| + process_path(options, controller, _path, option_path || _path) + end + + path_types.fetch(Symbol, []).each do |action| route_options = options.dup - route_options[:path] ||= _path if _path.is_a?(String) + decomposed_match(action, controller, route_options, option_path) + end - path_without_format = _path.to_s.sub(/\(\.:format\)$/, '') - if using_match_shorthand?(path_without_format, route_options) - route_options[:to] ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1') - route_options[:to].tr!("-", "_") - end + self + end + + def process_path(options, controller, path, option_path) + route_options = options.dup - decomposed_match(_path, controller, route_options) + path_without_format = path.sub(/\(\.:format\)$/, '') + if using_match_shorthand?(path_without_format, route_options) + route_options[:to] ||= path_without_format.gsub(%r{^/}, "").sub(%r{/([^/]*)$}, '#\1') + route_options[:to].tr!("-", "_") end - self + + decomposed_match(path, controller, route_options, option_path) end def using_match_shorthand?(path, options) path && (options[:to] || options[:action]).nil? && path =~ %r{^/?[-\w]+/[-\w/]+$} end - def decomposed_match(path, controller, options) # :nodoc: + def decomposed_match(path, controller, options, _path) # :nodoc: if on = options.delete(:on) - send(on) { decomposed_match(path, controller, options) } + send(on) { decomposed_match(path, controller, options, _path) } else case @scope.scope_level when :resources - nested { decomposed_match(path, controller, options) } + nested { decomposed_match(path, controller, options, _path) } when :resource - member { decomposed_match(path, controller, options) } + member { decomposed_match(path, controller, options, _path) } else - add_route(path, controller, options) + add_route(path, controller, options, _path) end end end - def add_route(action, controller, options) # :nodoc: - path = path_for_action(action, options.delete(:path)) + def add_route(action, controller, options, _path) # :nodoc: + path = path_for_action(action, _path) raise ArgumentError, "path is required" if path.blank? action = action.to_s -- cgit v1.2.3