aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-02-24 20:31:34 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-02-24 20:31:34 +0000
commitf95dfff966696cba237723e4abfefd3ae8ca91cb (patch)
tree8bfb23afa40836bfd1526b725cd54b058169452a /actionpack
parent351eea1fcd9619472165859534f655870518023d (diff)
downloadrails-f95dfff966696cba237723e4abfefd3ae8ca91cb.tar.gz
rails-f95dfff966696cba237723e4abfefd3ae8ca91cb.tar.bz2
rails-f95dfff966696cba237723e4abfefd3ae8ca91cb.zip
Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6221 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/helpers.rb26
-rw-r--r--actionpack/test/controller/helper_test.rb21
-rw-r--r--actionpack/test/fixtures/helpers/fun/pdf_helper.rb2
4 files changed, 45 insertions, 6 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 62aaa15c08..20258b536e 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH]
+
* Integration tests: introduce methods for other HTTP methods. #6353 [caboose]
* Routing: better support for escaped values in route segments. #7544 [Chris
diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/helpers.rb
index 4bea0c0fb9..ab5b945b4d 100644
--- a/actionpack/lib/action_controller/helpers.rb
+++ b/actionpack/lib/action_controller/helpers.rb
@@ -1,5 +1,7 @@
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
@@ -46,19 +48,28 @@ module ActionController #:nodoc:
end
# Declare a helper:
+ #
# helper :foo
# requires 'foo_helper' and includes FooHelper in the template class.
+ #
# helper FooHelper
# includes FooHelper in the template class.
+ #
# helper { def foo() "#{bar} is the very best" end }
# evaluates the block in the template class, adding method #foo.
+ #
# helper(:three, BlindHelper) { def mice() 'mice' end }
# does all three.
+ #
+ # helper :all
+ # includes all helpers from app/views/helpers/**/*.rb under RAILS_ROOT
def helper(*args, &block)
args.flatten.each do |arg|
case arg
when Module
add_template_helper(arg)
+ when :all
+ helper(all_application_helpers)
when String, Symbol
file_name = arg.to_s.underscore + '_helper'
class_name = file_name.camelize
@@ -73,14 +84,14 @@ module ActionController #:nodoc:
add_template_helper(class_name.constantize)
else
- raise ArgumentError, 'helper expects String, Symbol, or Module argument'
+ raise ArgumentError, "helper expects String, Symbol, or Module argument (was: #{args.inspect})"
end
end
# Evaluate block in template class if given.
master_helper_module.module_eval(&block) if block_given?
end
-
+
# Declare a controller method as a helper. For example,
# helper_method :link_to
# def link_to(name, options) ... end
@@ -104,6 +115,7 @@ module ActionController #:nodoc:
attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") }
end
+
private
def default_helper_module!
module_name = name.sub(/Controller$|$/, 'Helper')
@@ -120,6 +132,7 @@ module ActionController #:nodoc:
def inherited_with_helper(child)
inherited_without_helper(child)
+
begin
child.master_helper_module = Module.new
child.master_helper_module.send :include, master_helper_module
@@ -128,6 +141,13 @@ module ActionController #:nodoc:
raise unless e.is_missing?("helpers/#{child.controller_path}_helper")
end
end
+
+ def all_application_helpers
+ Dir["#{HELPERS_DIR}/**/*.rb"].collect do |file|
+ # Helper file without excess path, "_helper" suffix, and_extension
+ file[((File.dirname(HELPERS_DIR) + "/helpers/").size)..-("_helper".size + 4)]
+ end
+ end
end
end
-end
+end \ No newline at end of file
diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb
index 98c8f7e72c..54e2582ca2 100644
--- a/actionpack/test/controller/helper_test.rb
+++ b/actionpack/test/controller/helper_test.rb
@@ -1,5 +1,7 @@
require File.dirname(__FILE__) + '/../abstract_unit'
+silence_warnings { ActionController::Helpers::HELPERS_DIR = File.dirname(__FILE__) + '/../fixtures/helpers' }
+
class TestController < ActionController::Base
attr_accessor :delegate_attr
def delegate_method() end
@@ -15,7 +17,7 @@ module Fun
def rescue_action(e) raise end
end
- class PDFController < ActionController::Base
+ class PdfController < ActionController::Base
def test
render :inline => "test: <%= foobar %>"
end
@@ -24,6 +26,10 @@ module Fun
end
end
+class ApplicationController < ActionController::Base
+ helper :all
+end
+
module LocalAbcHelper
def a() end
def b() end
@@ -120,7 +126,18 @@ class HelperTest < Test::Unit::TestCase
response = ActionController::TestResponse.new
request.action = 'test'
- assert_equal 'test: baz', Fun::PDFController.process(request, response).body
+ assert_equal 'test: baz', Fun::PdfController.process(request, response).body
+ end
+
+ def test_all_helpers
+ # abc_helper.rb
+ assert ApplicationController.master_helper_module.instance_methods.include?("bare_a")
+
+ # fun/games_helper.rb
+ assert ApplicationController.master_helper_module.instance_methods.include?("stratego")
+
+ # fun/pdf_helper.rb
+ assert ApplicationController.master_helper_module.instance_methods.include?("foobar")
end
private
diff --git a/actionpack/test/fixtures/helpers/fun/pdf_helper.rb b/actionpack/test/fixtures/helpers/fun/pdf_helper.rb
index 1890f6c9ec..c4aea5a3f3 100644
--- a/actionpack/test/fixtures/helpers/fun/pdf_helper.rb
+++ b/actionpack/test/fixtures/helpers/fun/pdf_helper.rb
@@ -1,3 +1,3 @@
-module Fun::PDFHelper
+module Fun::PdfHelper
def foobar() 'baz' end
end