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

Changeset 6899

Show
Ignore:
Timestamp:
05/29/07 22:06:13 (1 year ago)
Author:
bitsweat
Message:

Don't choke on nested helpers.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/helpers.rb

    r6221 r6899  
    22  module Helpers #:nodoc: 
    33    HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") 
    4      
     4 
    55    def self.included(base) 
    66      # Initialize the base module to aggregate its helpers. 
     
    2020 
    2121    # The template helpers serve to relieve the templates from including the same inline code again and again. It's a 
    22     # set of standardized methods for working with forms (FormHelper), dates (DateHelper), texts (TextHelper), and  
     22    # set of standardized methods for working with forms (FormHelper), dates (DateHelper), texts (TextHelper), and 
    2323    # Active Records (ActiveRecordHelper) that's available to all templates by default. 
    2424    # 
    2525    # It's also really easy to make your own helpers and it's much encouraged to keep the template files free 
    26     # from complicated logic. It's even encouraged to bundle common compositions of methods from other helpers  
     26    # from complicated logic. It's even encouraged to bundle common compositions of methods from other helpers 
    2727    # (often the common helpers) as they're used by the specific application. 
    28     #  
     28    # 
    2929    #   module MyHelper 
    3030    #     def hello_world() "hello world" end 
    3131    #   end 
    32     #  
     32    # 
    3333    # MyHelper can now be included in a controller, like this: 
    34     #  
     34    # 
    3535    #   class MyController < ActionController::Base 
    3636    #     helper :my_helper 
    3737    #   end 
    38     #  
     38    # 
    3939    # ...and, same as above, used in any template rendered from MyController, like this: 
    40     #  
     40    # 
    4141    # Let's hear what the helper has to say: <tt><%= hello_world %></tt> 
    4242    module ClassMethods 
    4343      # Makes all the (instance) methods in the helper module available to templates rendered through this controller. 
    44       # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules  
     44      # See ActionView::Helpers (link:classes/ActionView/Helpers.html) for more about making your own helper modules 
    4545      # available to the templates. 
    4646      def add_template_helper(helper_module) #:nodoc: 
     
    7474              file_name  = arg.to_s.underscore + '_helper' 
    7575              class_name = file_name.camelize 
    76                  
     76 
    7777              begin 
    7878                require_dependency(file_name) 
     
    9292        master_helper_module.module_eval(&block) if block_given? 
    9393      end 
    94        
     94 
    9595      # Declare a controller method as a helper.  For example, 
    9696      #   helper_method :link_to 
     
    117117 
    118118 
    119       private  
     119      private 
    120120        def default_helper_module! 
    121121          module_name = name.sub(/Controller$|$/, 'Helper') 
     
    142142          end 
    143143        end 
    144          
     144 
     145        # Extract helper names from files in app/helpers/**/*.rb 
    145146        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 
     147          extract = /^#{Regexp.quote(HELPERS_DIR)}\/?(.*)_helper.rb$/ 
     148          Dir["#{HELPERS_DIR}/**/*_helper.rb"].map { |file| file.sub extract, '\1' } 
    150149        end 
    151150    end