diff options
Diffstat (limited to 'actionpack/lib/action_dispatch/journey/visitors.rb')
-rw-r--r-- | actionpack/lib/action_dispatch/journey/visitors.rb | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/actionpack/lib/action_dispatch/journey/visitors.rb b/actionpack/lib/action_dispatch/journey/visitors.rb index d9f634623d..616c2d26d4 100644 --- a/actionpack/lib/action_dispatch/journey/visitors.rb +++ b/actionpack/lib/action_dispatch/journey/visitors.rb @@ -107,10 +107,11 @@ module ActionDispatch # Used for formatting urls (url_for) class Formatter < Visitor # :nodoc: - attr_reader :options + attr_reader :options, :consumed def initialize(options) @options = options + @consumed = {} end private @@ -122,41 +123,41 @@ module ActionDispatch Router::Utils.escape_segment(value) end - def visit(node, optional = false) - case node.type - when :LITERAL, :SLASH, :DOT - node.left - when :STAR - visit_STAR(node.left) - when :GROUP - visit(node.left, true) - when :CAT - visit_CAT(node, optional) - when :SYMBOL - visit_SYMBOL(node, node.to_sym) + def visit_GROUP(node) + if consumed == options + nil + else + route = visit(node.left) + route.include?("\0") ? nil : route end end - def visit_CAT(node, optional) - left = visit(node.left, optional) - right = visit(node.right, optional) + def terminal(node) + node.left + end - if optional && !(right && left) - "" - else - [left, right].join - end + def binary(node) + [visit(node.left), visit(node.right)].join + end + + def nary(node) + node.children.map { |c| visit(c) }.join end def visit_STAR(node) - if value = options[node.to_sym] + if value = options[node.left.to_sym] escape_path(value) end end - def visit_SYMBOL(node, name) - if value = options[name] - name == :controller ? escape_path(value) : escape_segment(value) + def visit_SYMBOL(node) + key = node.to_sym + + if value = options[key] + consumed[key] = value + key == :controller ? escape_path(value) : escape_segment(value) + else + "\0" end end end |