diff options
author | Sam Pohlenz <sam@sampohlenz.com> | 2008-11-20 14:05:20 -0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2008-11-23 13:42:53 +0100 |
commit | 5ea9f2cac6e136e27d7fef2662412dda25391860 (patch) | |
tree | ee393f92f694e9c5c239b6195e37a807b4ed6fb8 | |
parent | 9e08a3bb1d47f79b6953056e72eee58e86d83ead (diff) | |
download | rails-5ea9f2cac6e136e27d7fef2662412dda25391860.tar.gz rails-5ea9f2cac6e136e27d7fef2662412dda25391860.tar.bz2 rails-5ea9f2cac6e136e27d7fef2662412dda25391860.zip |
Allow helpers directory to be overridden via ActionController::Base.helpers_dir (Sam Pohlenz) [#1424 state:committed]
Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/helpers.rb | 14 | ||||
-rw-r--r-- | actionpack/test/abstract_unit.rb | 1 | ||||
-rw-r--r-- | actionpack/test/controller/helper_test.rb | 16 | ||||
-rw-r--r-- | actionpack/test/fixtures/alternate_helpers/foo_helper.rb | 3 |
5 files changed, 29 insertions, 7 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 40655c6577..59a4bbea59 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *2.3.0 [Edge]* +* Allow helpers directory to be overridden via ActionController::Base.helpers_dir #1424 [Sam Pohlenz] + * Remove deprecated ActionController::Base#assign_default_content_type_and_charset * Changed the default of ActionView#render to assume partials instead of files when not given an options hash [DHH]. Examples: diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/helpers.rb index 9cffa07b26..6064931eb8 100644 --- a/actionpack/lib/action_controller/helpers.rb +++ b/actionpack/lib/action_controller/helpers.rb @@ -1,13 +1,15 @@ # FIXME: helper { ... } is broken on Ruby 1.9 module ActionController #:nodoc: module Helpers #:nodoc: - HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") - def self.included(base) # Initialize the base module to aggregate its helpers. base.class_inheritable_accessor :master_helper_module base.master_helper_module = Module.new + # Set the default directory for helpers + base.class_inheritable_accessor :helpers_dir + base.helpers_dir = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") + # Extend base with class methods to declare helpers. base.extend(ClassMethods) @@ -88,8 +90,8 @@ module ActionController #:nodoc: # When the argument is a module it will be included directly in the template class. # helper FooHelper # => includes FooHelper # - # When the argument is the symbol <tt>:all</tt>, the controller will include all helpers from - # <tt>app/helpers/**/*.rb</tt> under RAILS_ROOT. + # When the argument is the symbol <tt>:all</tt>, the controller will include all helpers beneath + # <tt>ActionController::Base.helpers_dir</tt> (defaults to <tt>app/helpers/**/*.rb</tt> under RAILS_ROOT). # helper :all # # Additionally, the +helper+ class method can receive and evaluate a block, making the methods defined available @@ -213,8 +215,8 @@ module ActionController #:nodoc: # Extract helper names from files in app/helpers/**/*.rb def all_application_helpers - extract = /^#{Regexp.quote(HELPERS_DIR)}\/?(.*)_helper.rb$/ - Dir["#{HELPERS_DIR}/**/*_helper.rb"].map { |file| file.sub extract, '\1' } + extract = /^#{Regexp.quote(helpers_dir)}\/?(.*)_helper.rb$/ + Dir["#{helpers_dir}/**/*_helper.rb"].map { |file| file.sub extract, '\1' } end end end diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 9623afa89d..76812b94df 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -1,6 +1,7 @@ $:.unshift(File.dirname(__FILE__) + '/../lib') $:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib') $:.unshift(File.dirname(__FILE__) + '/fixtures/helpers') +$:.unshift(File.dirname(__FILE__) + '/fixtures/alternate_helpers') require 'rubygems' require 'yaml' diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb index 83e3b085e7..5f36461b89 100644 --- a/actionpack/test/controller/helper_test.rb +++ b/actionpack/test/controller/helper_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -ActionController::Helpers::HELPERS_DIR.replace File.dirname(__FILE__) + '/../fixtures/helpers' +ActionController::Base.helpers_dir = File.dirname(__FILE__) + '/../fixtures/helpers' class TestController < ActionController::Base attr_accessor :delegate_attr @@ -130,6 +130,20 @@ class HelperTest < Test::Unit::TestCase assert methods.include?('foobar') end + def test_all_helpers_with_alternate_helper_dir + @controller_class.helpers_dir = File.dirname(__FILE__) + '/../fixtures/alternate_helpers' + + # Reload helpers + @controller_class.master_helper_module = Module.new + @controller_class.helper :all + + # helpers/abc_helper.rb should not be included + assert !master_helper_methods.include?('bare_a') + + # alternate_helpers/foo_helper.rb + assert master_helper_methods.include?('baz') + end + def test_helper_proxy methods = ApplicationController.helpers.methods.map(&:to_s) diff --git a/actionpack/test/fixtures/alternate_helpers/foo_helper.rb b/actionpack/test/fixtures/alternate_helpers/foo_helper.rb new file mode 100644 index 0000000000..a956fce6fa --- /dev/null +++ b/actionpack/test/fixtures/alternate_helpers/foo_helper.rb @@ -0,0 +1,3 @@ +module FooHelper + def baz() end +end |