diff options
author | Andrew White <andrew.white@unboxedconsulting.com> | 2016-01-20 17:59:13 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxedconsulting.com> | 2016-01-20 17:59:13 +0000 |
commit | 1eace9402ba9ad17b51552b5c61e25c3ced84475 (patch) | |
tree | 14e8a536b06fadce73ca9553cbddfee6ae049894 /actionpack/lib/action_dispatch/routing | |
parent | 4b507dff1e429c65b59cf6c506cdfa3adc44b141 (diff) | |
download | rails-1eace9402ba9ad17b51552b5c61e25c3ced84475.tar.gz rails-1eace9402ba9ad17b51552b5c61e25c3ced84475.tar.bz2 rails-1eace9402ba9ad17b51552b5c61e25c3ced84475.zip |
Fix marking of custom routes for Journey
The Mapper build_path method marks routes where path parameters are part
of a path segment as custom routes by altering the regular expression, e.g:
get '/foo-:bar', to: 'foo#bar'
There were some edge cases where certain constructs weren't being picked
up and this commit fixes those.
Fixes #23069.
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 522012063d..afbaa45d20 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -184,26 +184,32 @@ module ActionDispatch def build_path(ast, requirements, anchor) pattern = Journey::Path::Pattern.new(ast, requirements, JOINED_SEPARATORS, anchor) - # Get all the symbol nodes followed by literals that are not the - # dummy node. - symbols = ast.find_all { |n| - n.cat? && n.left.symbol? && n.right.cat? && n.right.left.literal? - }.map(&:left) - - # Get all the symbol nodes preceded by literals. - symbols.concat ast.find_all { |n| - n.cat? && n.left.literal? && n.right.cat? && n.right.left.symbol? - }.map { |n| n.right.left } - - symbols.each { |x| - x.regexp = /(?:#{Regexp.union(x.regexp, '-')})+/ + # Find all the symbol nodes that are adjacent to literal nodes and alter + # the regexp so that Journey will partition them into custom routes. + ast.find_all { |node| + next unless node.cat? + + if node.left.literal? && node.right.symbol? + symbol = node.right + elsif node.left.literal? && node.right.cat? && node.right.left.symbol? + symbol = node.right.left + elsif node.left.symbol? && node.right.literal? + symbol = node.left + elsif node.left.symbol? && node.right.cat? && node.right.left.literal? + symbol = node.left + else + next + end + + if symbol + symbol.regexp = /(?:#{Regexp.union(symbol.regexp, '-')})+/ + end } pattern end private :build_path - private def add_wildcard_options(options, formatted, path_ast) # Add a constraint for wildcard route to make it non-greedy and match the |