Changeset 6221
- Timestamp:
- 02/24/07 20:31:34 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/helpers.rb (modified) (8 diffs)
- trunk/actionpack/test/controller/helper_test.rb (modified) (4 diffs)
- trunk/actionpack/test/fixtures/helpers/fun/pdf_helper.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r6203 r6221 1 1 *SVN* 2 3 * Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH] 2 4 3 5 * Integration tests: introduce methods for other HTTP methods. #6353 [caboose] trunk/actionpack/lib/action_controller/helpers.rb
r6052 r6221 1 1 module ActionController #:nodoc: 2 2 module Helpers #:nodoc: 3 HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") 4 3 5 def self.included(base) 4 6 # Initialize the base module to aggregate its helpers. … … 47 49 48 50 # Declare a helper: 51 # 49 52 # helper :foo 50 53 # requires 'foo_helper' and includes FooHelper in the template class. 54 # 51 55 # helper FooHelper 52 56 # includes FooHelper in the template class. 57 # 53 58 # helper { def foo() "#{bar} is the very best" end } 54 59 # evaluates the block in the template class, adding method #foo. 60 # 55 61 # helper(:three, BlindHelper) { def mice() 'mice' end } 56 62 # does all three. 63 # 64 # helper :all 65 # includes all helpers from app/views/helpers/**/*.rb under RAILS_ROOT 57 66 def helper(*args, &block) 58 67 args.flatten.each do |arg| … … 60 69 when Module 61 70 add_template_helper(arg) 71 when :all 72 helper(all_application_helpers) 62 73 when String, Symbol 63 74 file_name = arg.to_s.underscore + '_helper' … … 74 85 add_template_helper(class_name.constantize) 75 86 else 76 raise ArgumentError, 'helper expects String, Symbol, or Module argument'87 raise ArgumentError, "helper expects String, Symbol, or Module argument (was: #{args.inspect})" 77 88 end 78 89 end … … 81 92 master_helper_module.module_eval(&block) if block_given? 82 93 end 83 94 84 95 # Declare a controller method as a helper. For example, 85 96 # helper_method :link_to … … 105 116 end 106 117 118 107 119 private 108 120 def default_helper_module! … … 121 133 def inherited_with_helper(child) 122 134 inherited_without_helper(child) 135 123 136 begin 124 137 child.master_helper_module = Module.new … … 129 142 end 130 143 end 144 145 def all_application_helpers 146 Dir["#{HELPERS_DIR}/**/*.rb"].collect do |file| 147 # Helper file without excess path, "_helper" suffix, and_extension 148 file[((File.dirname(HELPERS_DIR) + "/helpers/").size)..-("_helper".size + 4)] 149 end 150 end 131 151 end 132 152 end trunk/actionpack/test/controller/helper_test.rb
r4595 r6221 1 1 require File.dirname(__FILE__) + '/../abstract_unit' 2 3 silence_warnings { ActionController::Helpers::HELPERS_DIR = File.dirname(__FILE__) + '/../fixtures/helpers' } 2 4 3 5 class TestController < ActionController::Base … … 16 18 end 17 19 18 class P DFController < ActionController::Base20 class PdfController < ActionController::Base 19 21 def test 20 22 render :inline => "test: <%= foobar %>" … … 23 25 def rescue_action(e) raise end 24 26 end 27 end 28 29 class ApplicationController < ActionController::Base 30 helper :all 25 31 end 26 32 … … 121 127 request.action = 'test' 122 128 123 assert_equal 'test: baz', Fun::PDFController.process(request, response).body 129 assert_equal 'test: baz', Fun::PdfController.process(request, response).body 130 end 131 132 def test_all_helpers 133 # abc_helper.rb 134 assert ApplicationController.master_helper_module.instance_methods.include?("bare_a") 135 136 # fun/games_helper.rb 137 assert ApplicationController.master_helper_module.instance_methods.include?("stratego") 138 139 # fun/pdf_helper.rb 140 assert ApplicationController.master_helper_module.instance_methods.include?("foobar") 124 141 end 125 142 trunk/actionpack/test/fixtures/helpers/fun/pdf_helper.rb
r2938 r6221 1 module Fun::P DFHelper1 module Fun::PdfHelper 2 2 def foobar() 'baz' end 3 3 end