diff options
-rw-r--r-- | actionpack/CHANGELOG | 3 | ||||
-rw-r--r-- | actionpack/lib/action_controller/helpers.rb | 9 | ||||
-rw-r--r-- | actionpack/test/controller/helper_test.rb | 16 |
3 files changed, 28 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 96c38f0e6e..fe9e2ebd5f 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,8 @@ *SVN* +* Provide a helper proxy to access helper methods from outside views. Closes #10839 [Josh Peek] + e.g. ApplicationController.helpers.simple_format(text) + * Improve documentation. [Xavier Noria, leethal, jerome] * Ensure RJS redirect_to doesn't html-escapes string argument. Closes #8546 [josh, eventualbuddha, Pratik] diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/helpers.rb index 81b8ff9753..9188f94f37 100644 --- a/actionpack/lib/action_controller/helpers.rb +++ b/actionpack/lib/action_controller/helpers.rb @@ -167,6 +167,15 @@ module ActionController #:nodoc: attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") } end + # Provides a proxy to access helpers methods from outside the view. + def helpers + unless @helper_proxy + @helper_proxy = ActionView::Base.new + @helper_proxy.extend master_helper_module + else + @helper_proxy + end + end private def default_helper_module! diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb index d18388821b..6dc77a4aaf 100644 --- a/actionpack/test/controller/helper_test.rb +++ b/actionpack/test/controller/helper_test.rb @@ -130,6 +130,22 @@ class HelperTest < Test::Unit::TestCase assert methods.include?('foobar') end + def test_helper_proxy + methods = ApplicationController.helpers.methods.map(&:to_s) + + # ActionView + assert methods.include?('pluralize') + + # abc_helper.rb + assert methods.include?('bare_a') + + # fun/games_helper.rb + assert methods.include?('stratego') + + # fun/pdf_helper.rb + assert methods.include?('foobar') + end + private def expected_helper_methods TestHelper.instance_methods.map(&:to_s) |