diff options
author | Andrew White <andrew.white@unboxedconsulting.com> | 2016-01-20 14:40:34 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2017-02-21 15:30:46 +0000 |
commit | 31dc46cb9c8aa3e05dc955ae50ec53421951b4a5 (patch) | |
tree | efbd45b034b330caf5e93a0d15325464d9c22329 | |
parent | 658b5244356feba2b262e87d8b333c5a46999a5d (diff) | |
download | rails-31dc46cb9c8aa3e05dc955ae50ec53421951b4a5.tar.gz rails-31dc46cb9c8aa3e05dc955ae50ec53421951b4a5.tar.bz2 rails-31dc46cb9c8aa3e05dc955ae50ec53421951b4a5.zip |
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.
-rw-r--r-- | actionpack/lib/action_dispatch/routing/route_set.rb | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 5b873aeab7..8ccfab56cf 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -452,17 +452,34 @@ module ActionDispatch # Define url_for in the singleton level so one can do: # Rails.application.routes.url_helpers.url_for(args) - @_routes = routes + proxy_class = Class.new do + include UrlFor + include routes.named_routes.path_helpers_module + include routes.named_routes.url_helpers_module + + attr_reader :_routes + + def initialize(routes) + @_routes = routes + end + + def optimize_routes_generation? + @_routes.optimize_routes_generation? + end + end + + @_proxy = proxy_class.new(routes) + class << self def url_for(options) - @_routes.url_for(options) + @_proxy.url_for(options) end def optimize_routes_generation? - @_routes.optimize_routes_generation? + @_proxy.optimize_routes_generation? end - attr_reader :_routes + def _routes; @_proxy._routes; end def url_options; {}; end end |