public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
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>
spohlenz (author)
Thu Nov 20 14:05:20 -0800 2008
dhh (committer)
Sun Nov 23 04:41:52 -0800 2008
commit  bdf995bc5da016e99d1636e62b39c92384263a9c
tree    ee393f92f694e9c5c239b6195e37a807b4ed6fb8
parent  9e08a3bb1d47f79b6953056e72eee58e86d83ead
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *2.3.0 [Edge]*
0
 
0
+* Allow helpers directory to be overridden via ActionController::Base.helpers_dir #1424 [Sam Pohlenz]
0
+
0
 * Remove deprecated ActionController::Base#assign_default_content_type_and_charset
0
 
0
 * Changed the default of ActionView#render to assume partials instead of files when not given an options hash [DHH]. Examples:
...
1
2
3
4
5
6
7
8
9
10
 
 
 
 
11
12
13
...
88
89
90
91
92
 
 
93
94
95
...
213
214
215
216
217
 
 
218
219
220
...
1
2
3
 
 
4
5
6
7
8
9
10
11
12
13
14
15
...
90
91
92
 
 
93
94
95
96
97
...
215
216
217
 
 
218
219
220
221
222
0
@@ -1,13 +1,15 @@
0
 # FIXME: helper { ... } is broken on Ruby 1.9
0
 module ActionController #:nodoc:
0
   module Helpers #:nodoc:
0
-    HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers")
0
-
0
     def self.included(base)
0
       # Initialize the base module to aggregate its helpers.
0
       base.class_inheritable_accessor :master_helper_module
0
       base.master_helper_module = Module.new
0
 
0
+      # Set the default directory for helpers
0
+      base.class_inheritable_accessor :helpers_dir
0
+      base.helpers_dir = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers")
0
+
0
       # Extend base with class methods to declare helpers.
0
       base.extend(ClassMethods)
0
 
0
@@ -88,8 +90,8 @@ module ActionController #:nodoc:
0
       # When the argument is a module it will be included directly in the template class.
0
       #   helper FooHelper # => includes FooHelper
0
       #
0
-      # When the argument is the symbol <tt>:all</tt>, the controller will include all helpers from 
0
-      # <tt>app/helpers/**/*.rb</tt> under RAILS_ROOT.
0
+      # When the argument is the symbol <tt>:all</tt>, the controller will include all helpers beneath
0
+      # <tt>ActionController::Base.helpers_dir</tt> (defaults to <tt>app/helpers/**/*.rb</tt> under RAILS_ROOT).
0
       #   helper :all
0
       #
0
       # Additionally, the +helper+ class method can receive and evaluate a block, making the methods defined available 
0
@@ -213,8 +215,8 @@ module ActionController #:nodoc:
0
 
0
         # Extract helper names from files in app/helpers/**/*.rb
0
         def all_application_helpers
0
-          extract = /^#{Regexp.quote(HELPERS_DIR)}\/?(.*)_helper.rb$/
0
-          Dir["#{HELPERS_DIR}/**/*_helper.rb"].map { |file| file.sub extract, '\1' }
0
+          extract = /^#{Regexp.quote(helpers_dir)}\/?(.*)_helper.rb$/
0
+          Dir["#{helpers_dir}/**/*_helper.rb"].map { |file| file.sub extract, '\1' }
0
         end
0
     end
0
   end
...
1
2
3
 
4
5
6
...
1
2
3
4
5
6
7
0
@@ -1,6 +1,7 @@
0
 $:.unshift(File.dirname(__FILE__) + '/../lib')
0
 $:.unshift(File.dirname(__FILE__) + '/../../activesupport/lib')
0
 $:.unshift(File.dirname(__FILE__) + '/fixtures/helpers')
0
+$:.unshift(File.dirname(__FILE__) + '/fixtures/alternate_helpers')
0
 
0
 require 'rubygems'
0
 require 'yaml'
...
1
2
3
 
4
5
6
...
130
131
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
134
135
...
1
2
 
3
4
5
6
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
0
@@ -1,6 +1,6 @@
0
 require 'abstract_unit'
0
 
0
-ActionController::Helpers::HELPERS_DIR.replace File.dirname(__FILE__) + '/../fixtures/helpers'
0
+ActionController::Base.helpers_dir = File.dirname(__FILE__) + '/../fixtures/helpers'
0
 
0
 class TestController < ActionController::Base
0
   attr_accessor :delegate_attr
0
@@ -130,6 +130,20 @@ class HelperTest < Test::Unit::TestCase
0
     assert methods.include?('foobar')
0
   end
0
 
0
+  def test_all_helpers_with_alternate_helper_dir
0
+    @controller_class.helpers_dir = File.dirname(__FILE__) + '/../fixtures/alternate_helpers'
0
+
0
+    # Reload helpers
0
+    @controller_class.master_helper_module = Module.new
0
+    @controller_class.helper :all
0
+
0
+    # helpers/abc_helper.rb should not be included
0
+    assert !master_helper_methods.include?('bare_a')
0
+
0
+    # alternate_helpers/foo_helper.rb
0
+    assert master_helper_methods.include?('baz')
0
+  end
0
+
0
   def test_helper_proxy
0
     methods = ApplicationController.helpers.methods.map(&:to_s)
0
 

Comments

Roman2K Sat Nov 29 06:05:25 -0800 2008

That a step in the right direction. How about allowing for multiple helper directories? I have written a plug-in that serves this purpose: ac-multi-helpers-dirs