From f66c2238b83ec8c382c12c8c4acf79bd2ecc7a25 Mon Sep 17 00:00:00 2001 From: Tom Lea Date: Fri, 31 Oct 2008 20:04:47 +0000 Subject: [PATCH] Because the ActionView has no reference to the default_url_options, it must delegate optomised named route url generators to the relevent controller. We can tell a view from a controller, because a view has a reference to the controller. We exploit this to ensure the correct methods are placed on the correct classes, without breaking the API. --- .../lib/action_controller/routing/route_set.rb | 27 +++++++++++++++++-- 1 files changed, 24 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb index ff44849..9952617 100644 --- a/actionpack/lib/action_controller/routing/route_set.rb +++ b/actionpack/lib/action_controller/routing/route_set.rb @@ -112,14 +112,29 @@ module ActionController end end - def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false) + def install(destinations = [ActionController::Base], regenerate = false) reset! if regenerate Array(destinations).each do |dest| dest.__send__(:include, @module) end end + def install_delegators(destinations = [ActionView::Base], delegation_method = :controller, regenerate = false) + reset! if regenerate + Array(destinations).each do |dest| + dest.__send__(:include, generate_delegation_module(delegation_method)) + end + end + private + def generate_delegation_module(to = :controller) + returning(Module.new) do |delegator_module| + @module.instance_methods.each do |method| + delegator_module.module_eval{ delegate method, :to => to } + end + end + end + def url_helper_name(name, kind = :url) :"#{name}_#{kind}" end @@ -221,8 +236,14 @@ module ActionController end def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) - Array(destinations).each { |d| d.module_eval { include Helpers } } - named_routes.install(destinations, regenerate_code) + Array(destinations).each {|d| + d.module_eval { include Helpers } + if d.instance_methods.include? "controller" + named_routes.install_delegators(d, :controller, regenerate_code) + else + named_routes.install(d, regenerate_code) + end + } end def empty? -- 1.5.5.4