From c7664d112fadb313146da33f48d1da318f249927 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 30 Jul 2010 07:14:48 +0200 Subject: Include application's helpers and router helpers by default, but include engine's ones for controllers inside isolated namespace --- actionpack/lib/action_controller/base.rb | 8 ++++++-- actionpack/lib/action_controller/metal/helpers.rb | 6 +++++- actionpack/lib/action_controller/railtie.rb | 3 ++- .../lib/action_controller/railties/routes_helpers.rb | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 actionpack/lib/action_controller/railties/routes_helpers.rb (limited to 'actionpack/lib/action_controller') diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 7a1464c2aa..3560ac5b8c 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -223,9 +223,13 @@ module ActionController def self.inherited(klass) super - klass.helper :all if klass.superclass == ActionController::Base + if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) } + klass.helper(all_helpers_from_path(namespace._railtie.config.paths.app.helpers.to_a)) + else + klass.helper :all if klass.superclass == ActionController::Base + end end ActiveSupport.run_load_hooks(:action_controller, self) end -end \ No newline at end of file +end diff --git a/actionpack/lib/action_controller/metal/helpers.rb b/actionpack/lib/action_controller/metal/helpers.rb index 4b6897c5dd..c5d7842db3 100644 --- a/actionpack/lib/action_controller/metal/helpers.rb +++ b/actionpack/lib/action_controller/metal/helpers.rb @@ -101,8 +101,12 @@ module ActionController # Extract helper names from files in app/helpers/**/*_helper.rb def all_application_helpers + all_helpers_from_path(helpers_path) + end + + def all_helpers_from_path(path) helpers = [] - Array.wrap(helpers_path).each do |path| + Array.wrap(path).each do |path| extract = /^#{Regexp.quote(path.to_s)}\/?(.*)_helper.rb$/ helpers += Dir["#{path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') } end diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 7496dd57b2..23622b19e8 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -4,6 +4,7 @@ require "action_dispatch/railtie" require "action_view/railtie" require "active_support/deprecation/proxy_wrappers" require "active_support/deprecation" +require "action_controller/railties/routes_helpers" module ActionController class Railtie < Rails::Railtie @@ -50,7 +51,7 @@ module ActionController options.helpers_path ||= paths.app.helpers.to_a ActiveSupport.on_load(:action_controller) do - include app.routes.url_helpers + extend ::ActionController::Railties::RoutesHelpers.with(app.routes) include app.routes.mounted_helpers(:app) options.each { |k,v| send("#{k}=", v) } end diff --git a/actionpack/lib/action_controller/railties/routes_helpers.rb b/actionpack/lib/action_controller/railties/routes_helpers.rb new file mode 100644 index 0000000000..a23f703f0b --- /dev/null +++ b/actionpack/lib/action_controller/railties/routes_helpers.rb @@ -0,0 +1,17 @@ +module ActionController + module Railties + module RoutesHelpers + def self.with(routes) + Module.new do + define_method(:inherited) do |klass| + super(klass) + if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) } + routes = namespace._railtie.routes + end + klass.send(:include, routes.url_helpers) + end + end + end + end + end +end -- cgit v1.2.3