Skip to content

Commit a624309

Browse files
committed
Wrap routes.url_helpers.url_for via a proxy
The singleton url_for on Rails.application.routes.url_helpers isn't the same as the url_for you get when you include the module in your class as the latter has support for polymorphic style routes, etc. whereas the former accepts only a hash and is the underlying implementation defined on ActionDispatch::Routing::RouteSet. This commit changes the singleton method to call through a proxy instance so that it gets the full range of features specified in the documentation for url_for.
1 parent 6e5e8ba commit a624309

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

actionpack/lib/action_dispatch/routing/route_set.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,17 +452,34 @@ def url_helpers(supports_path = true)
452452

453453
# Define url_for in the singleton level so one can do:
454454
# Rails.application.routes.url_helpers.url_for(args)
455-
@_routes = routes
455+
proxy_class = Class.new do
456+
include UrlFor
457+
include routes.named_routes.path_helpers_module
458+
include routes.named_routes.url_helpers_module
459+
460+
attr_reader :_routes
461+
462+
def initialize(routes)
463+
@_routes = routes
464+
end
465+
466+
def optimize_routes_generation?
467+
@_routes.optimize_routes_generation?
468+
end
469+
end
470+
471+
@_proxy = proxy_class.new(routes)
472+
456473
class << self
457474
def url_for(options)
458-
@_routes.url_for(options)
475+
@_proxy.url_for(options)
459476
end
460477

461478
def optimize_routes_generation?
462-
@_routes.optimize_routes_generation?
479+
@_proxy.optimize_routes_generation?
463480
end
464481

465-
attr_reader :_routes
482+
def _routes; @_proxy._routes; end
466483
def url_options; {}; end
467484
end
468485

0 commit comments

Comments
 (0)