Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6221

Show
Ignore:
Timestamp:
02/24/07 20:31:34 (2 years ago)
Author:
david
Message:

Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r6203 r6221  
    11*SVN* 
     2 
     3* Added helper(:all) as a way to include all helpers from app/helpers/**/*.rb in ApplicationController [DHH] 
    24 
    35* Integration tests: introduce methods for other HTTP methods.  #6353 [caboose] 
  • trunk/actionpack/lib/action_controller/helpers.rb

    r6052 r6221  
    11module ActionController #:nodoc: 
    22  module Helpers #:nodoc: 
     3    HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") 
     4     
    35    def self.included(base) 
    46      # Initialize the base module to aggregate its helpers. 
     
    4749 
    4850      # Declare a helper: 
     51      # 
    4952      #   helper :foo 
    5053      # requires 'foo_helper' and includes FooHelper in the template class. 
     54      # 
    5155      #   helper FooHelper 
    5256      # includes FooHelper in the template class. 
     57      # 
    5358      #   helper { def foo() "#{bar} is the very best" end } 
    5459      # evaluates the block in the template class, adding method #foo. 
     60      # 
    5561      #   helper(:three, BlindHelper) { def mice() 'mice' end } 
    5662      # does all three. 
     63      # 
     64      #   helper :all 
     65      # includes all helpers from app/views/helpers/**/*.rb under RAILS_ROOT 
    5766      def helper(*args, &block) 
    5867        args.flatten.each do |arg| 
     
    6069            when Module 
    6170              add_template_helper(arg) 
     71            when :all 
     72              helper(all_application_helpers) 
    6273            when String, Symbol 
    6374              file_name  = arg.to_s.underscore + '_helper' 
     
    7485              add_template_helper(class_name.constantize) 
    7586            else 
    76               raise ArgumentError, 'helper expects String, Symbol, or Module argument' 
     87              raise ArgumentError, "helper expects String, Symbol, or Module argument (was: #{args.inspect})" 
    7788          end 
    7889        end 
     
    8192        master_helper_module.module_eval(&block) if block_given? 
    8293      end 
    83  
     94       
    8495      # Declare a controller method as a helper.  For example, 
    8596      #   helper_method :link_to 
     
    105116      end 
    106117 
     118 
    107119      private  
    108120        def default_helper_module! 
     
    121133        def inherited_with_helper(child) 
    122134          inherited_without_helper(child) 
     135 
    123136          begin 
    124137            child.master_helper_module = Module.new 
     
    129142          end 
    130143        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 
    131151    end 
    132152  end 
  • trunk/actionpack/test/controller/helper_test.rb

    r4595 r6221  
    11require File.dirname(__FILE__) + '/../abstract_unit' 
     2 
     3silence_warnings { ActionController::Helpers::HELPERS_DIR = File.dirname(__FILE__) + '/../fixtures/helpers' } 
    24 
    35class TestController < ActionController::Base 
     
    1618  end 
    1719 
    18   class PDFController < ActionController::Base 
     20  class PdfController < ActionController::Base 
    1921    def test 
    2022      render :inline => "test: <%= foobar %>" 
     
    2325    def rescue_action(e) raise end 
    2426  end 
     27end 
     28 
     29class ApplicationController < ActionController::Base 
     30  helper :all 
    2531end 
    2632 
     
    121127    request.action = 'test' 
    122128 
    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") 
    124141  end 
    125142 
  • trunk/actionpack/test/fixtures/helpers/fun/pdf_helper.rb

    r2938 r6221  
    1 module Fun::PDFHelper 
     1module Fun::PdfHelper 
    22  def foobar() 'baz' end 
    33end