Skip to content

Commit

Permalink
Ensure routing optimizations are cleared when new routes are added [#981
Browse files Browse the repository at this point in the history
 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
al2o3cr authored and josh committed Sep 7, 2008
1 parent d4eb3c0 commit d4ef590
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ def recognize_path(path, environment={})
end
end

def recognize_optimized(path, env)
write_recognize_optimized
recognize_optimized(path, env)
def clear_recognize_optimized!
instance_eval %{
def recognize_optimized(path, env)
write_recognize_optimized!
recognize_optimized(path, env)
end
}, __FILE__, __LINE__
end

def write_recognize_optimized
def write_recognize_optimized!
tree = segment_tree(routes)
body = generate_code(tree)
instance_eval %{
Expand Down
5 changes: 3 additions & 2 deletions actionpack/lib/action_controller/routing/route_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def #{selector}(*args)
def initialize
self.routes = []
self.named_routes = NamedRouteCollection.new
clear_recognize_optimized!
end

# Subclasses and plugins may override this method to specify a different
Expand All @@ -213,9 +214,9 @@ def clear!
named_routes.clear
@combined_regexp = nil
@routes_by_controller = nil
# This will force routing/recognition_optimization.rb
# This will force routing/recognition_optimisation.rb
# to refresh optimisations.
@compiled_recognize_optimized = nil
clear_recognize_optimized!
end

def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
Expand Down
29 changes: 29 additions & 0 deletions actionpack/test/controller/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,35 @@ def test_failed_requirements_raises_exception_with_violated_requirements
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 SegmentTest < Test::Unit::TestCase
Expand Down

0 comments on commit d4ef590

Please sign in to comment.