From 7e6cda15f8dae517f9605f73aa1c966a29d4930a Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Sun, 7 Sep 2008 10:21:34 -0500 Subject: [PATCH] Ensure routing optimizations are cleared when new routes are added [#981 state:resolved] Signed-off-by: Joshua Peek --- .../routing/recognition_optimisation.rb | 9 ++++++ .../action_controller/routing/route_set.rb | 4 +-- actionpack/test/controller/routing_test.rb | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/routing/recognition_optimisation.rb b/actionpack/lib/action_controller/routing/recognition_optimisation.rb index 6d54d0334c..306219a431 100644 --- a/actionpack/lib/action_controller/routing/recognition_optimisation.rb +++ b/actionpack/lib/action_controller/routing/recognition_optimisation.rb @@ -147,6 +147,15 @@ module ActionController end }, __FILE__, __LINE__ end + + def clear_recognize_optimized! + instance_eval %{ + def recognize_optimized(path, environment) + write_recognize_optimized! + recognize_optimized(path, environment) + end + }, __FILE__, __LINE__ + end end end end diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index 9d48f289db..ff448490e9 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -195,7 +195,7 @@ module ActionController self.routes = [] self.named_routes = NamedRouteCollection.new - write_recognize_optimized! + clear_recognize_optimized! end # Subclasses and plugins may override this method to specify a different @@ -217,7 +217,7 @@ module ActionController @routes_by_controller = nil # This will force routing/recognition_optimization.rb # to refresh optimisations. - @compiled_recognize_optimized = nil + clear_recognize_optimized! end def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 8bb1c49cbd..1eb26a7cfd 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -1375,6 +1375,36 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do x.send(:foo_with_requirement_url, "I am Against the requirements") end end + + def test_routes_changed_correctly_after_clear + ActionController::Base.optimise_named_routes = true + rs = ::ActionController::Routing::RouteSet.new + rs.draw do |r| + r.connect 'ca', :controller => 'ca', :action => "aa" + r.connect 'cb', :controller => 'cb', :action => "ab" + r.connect 'cc', :controller => 'cc', :action => "ac" + r.connect ':controller/:action/:id' + r.connect ':controller/:action/:id.:format' + end + + hash = rs.recognize_path "/cc" + + assert_not_nil hash + assert_equal %w(cc ac), [hash[:controller], hash[:action]] + + rs.draw do |r| + r.connect 'cb', :controller => 'cb', :action => "ab" + r.connect 'cc', :controller => 'cc', :action => "ac" + r.connect ':controller/:action/:id' + r.connect ':controller/:action/:id.:format' + end + + hash = rs.recognize_path "/cc" + + assert_not_nil hash + assert_equal %w(cc ac), [hash[:controller], hash[:action]] + + end end class RouteTest < Test::Unit::TestCase