From 86cf7a2d166430fac1611aa7593b52b46eeb9f70 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Sun, 3 Mar 2013 19:18:01 +0000 Subject: Use custom visitor class for optimized url helpers Rather than trying to use gsub to remove the optional route segments, which will fail with nested optional segments, use a custom visitor class that returns a empty string for group nodes. Closes #9524 --- actionpack/lib/action_dispatch/journey/route.rb | 4 ++++ actionpack/lib/action_dispatch/journey/visitors.rb | 8 ++++++++ actionpack/lib/action_dispatch/routing/route_set.rb | 10 +--------- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'actionpack/lib/action_dispatch') diff --git a/actionpack/lib/action_dispatch/journey/route.rb b/actionpack/lib/action_dispatch/journey/route.rb index 063302e0f2..6fda085681 100644 --- a/actionpack/lib/action_dispatch/journey/route.rb +++ b/actionpack/lib/action_dispatch/journey/route.rb @@ -71,6 +71,10 @@ module ActionDispatch Visitors::Formatter.new(path_options).accept(path.spec) end + def optimized_path + Visitors::OptimizedPath.new.accept(path.spec) + end + def optional_parts path.optional_names.map { |n| n.to_sym } end diff --git a/actionpack/lib/action_dispatch/journey/visitors.rb b/actionpack/lib/action_dispatch/journey/visitors.rb index 46bd58c178..2964d80d9f 100644 --- a/actionpack/lib/action_dispatch/journey/visitors.rb +++ b/actionpack/lib/action_dispatch/journey/visitors.rb @@ -74,6 +74,14 @@ module ActionDispatch end end + class OptimizedPath < String # :nodoc: + private + + def visit_GROUP(node) + "" + end + end + # Used for formatting urls (url_for) class Formatter < Visitor # :nodoc: attr_reader :options, :consumed diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index ca31b5e02e..619dd22ec1 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -165,7 +165,7 @@ module ActionDispatch super @path_parts = @route.required_parts @arg_size = @path_parts.size - @string_route = string_route(route) + @string_route = @route.optimized_path end def call(t, args) @@ -180,14 +180,6 @@ module ActionDispatch private - def string_route(route) - string_route = route.ast.to_s.dup - while string_route.gsub!(/\([^\)]*\)/, "") - true - end - string_route - end - def optimized_helper(args) path = @string_route.dup klass = Journey::Router::Utils -- cgit v1.2.3