diff options
-rw-r--r-- | actionpack/CHANGELOG.md | 7 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 8 | ||||
-rw-r--r-- | railties/test/application/routing_test.rb | 58 |
3 files changed, 69 insertions, 4 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 73c30aa9b0..869fc7d484 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,3 +1,10 @@ +* Prefer `remove_method` over `undef_method` when reloading routes + + When `undef_method` is used it prevents access to other implementations of that + url helper in the ancestor chain so use `remove_method` instead to restores access. + + *Andrew White* + * Add the `direct` method to the routing DSL This new method allows customization of the routing behavior in two ways: diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 8bdf0d1a53..7cab421887 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -89,11 +89,11 @@ module ActionDispatch def clear! @path_helpers.each do |helper| - @path_helpers_module.send :undef_method, helper + @path_helpers_module.send :remove_method, helper end @url_helpers.each do |helper| - @url_helpers_module.send :undef_method, helper + @url_helpers_module.send :remove_method, helper end @custom_helpers.each do |helper| @@ -101,11 +101,11 @@ module ActionDispatch url_name = :"#{helper}_url" if @path_helpers_module.method_defined?(path_name) - @path_helpers_module.send :undef_method, path_name + @path_helpers_module.send :remove_method, path_name end if @url_helpers_module.method_defined?(url_name) - @url_helpers_module.send :undef_method, url_name + @url_helpers_module.send :remove_method, url_name end end diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 6c34c72564..1132e7fb55 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -618,5 +618,63 @@ module ApplicationTests get "/yazilar" assert_equal 200, last_response.status end + + test "reloading routes removes methods and doesn't undefine them" do + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + get '/url', to: 'url#index' + end + RUBY + + app_file "app/models/url_helpers.rb", <<-RUBY + module UrlHelpers + def foo_path + "/foo" + end + end + RUBY + + app_file "app/models/context.rb", <<-RUBY + class Context + include UrlHelpers + include Rails.application.routes.url_helpers + end + RUBY + + controller "url", <<-RUBY + class UrlController < ApplicationController + def index + context = Context.new + render plain: context.foo_path + end + end + RUBY + + get "/url" + assert_equal "/foo", last_response.body + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + get '/url', to: 'url#index' + get '/bar', to: 'foo#index', as: 'foo' + end + RUBY + + Rails.application.reload_routes! + + get "/url" + assert_equal "/bar", last_response.body + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + get '/url', to: 'url#index' + end + RUBY + + Rails.application.reload_routes! + + get "/url" + assert_equal "/foo", last_response.body + end end end |