Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear url helper methods when routes are reloaded
Remove all the old url helper methods when clear! is called on the route set because it's possible that some routes have been removed.
- Loading branch information
Showing
4 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require 'abstract_unit' | ||
|
||
module ActionDispatch | ||
module Routing | ||
class RouteSetTest < ActiveSupport::TestCase | ||
class SimpleApp | ||
def initialize(response) | ||
@response = response | ||
end | ||
|
||
def call(env) | ||
[ 200, { 'Content-Type' => 'text/plain' }, [response] ] | ||
end | ||
end | ||
|
||
setup do | ||
@set = RouteSet.new | ||
end | ||
|
||
test "url helpers are added when route is added" do | ||
draw do | ||
get 'foo', to: SimpleApp.new('foo#index') | ||
end | ||
|
||
assert_equal '/foo', url_helpers.foo_path | ||
assert_raises NoMethodError do | ||
assert_equal '/bar', url_helpers.bar_path | ||
end | ||
|
||
draw do | ||
get 'foo', to: SimpleApp.new('foo#index') | ||
get 'bar', to: SimpleApp.new('bar#index') | ||
end | ||
|
||
assert_equal '/foo', url_helpers.foo_path | ||
assert_equal '/bar', url_helpers.bar_path | ||
end | ||
|
||
test "url helpers are updated when route is updated" do | ||
draw do | ||
get 'bar', to: SimpleApp.new('bar#index'), as: :bar | ||
end | ||
|
||
assert_equal '/bar', url_helpers.bar_path | ||
|
||
draw do | ||
get 'baz', to: SimpleApp.new('baz#index'), as: :bar | ||
end | ||
|
||
assert_equal '/baz', url_helpers.bar_path | ||
end | ||
|
||
test "url helpers are removed when route is removed" do | ||
draw do | ||
get 'foo', to: SimpleApp.new('foo#index') | ||
get 'bar', to: SimpleApp.new('bar#index') | ||
end | ||
|
||
assert_equal '/foo', url_helpers.foo_path | ||
assert_equal '/bar', url_helpers.bar_path | ||
|
||
draw do | ||
get 'foo', to: SimpleApp.new('foo#index') | ||
end | ||
|
||
assert_equal '/foo', url_helpers.foo_path | ||
assert_raises NoMethodError do | ||
assert_equal '/bar', url_helpers.bar_path | ||
end | ||
end | ||
|
||
private | ||
def clear! | ||
@set.clear! | ||
end | ||
|
||
def draw(&block) | ||
@set.draw(&block) | ||
end | ||
|
||
def url_helpers | ||
@set.url_helpers | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How about iterating inside the module_eval block instead?
Might yield some performance difference according to my tests. Btw, can't
remove_possible_method
be called withoutmodule_eval
? I think it's public so should work?