Changeset 6899
- Timestamp:
- 05/29/07 22:06:13 (1 year ago)
- Files:
-
- trunk/actionpack/lib/action_controller/helpers.rb (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller/helpers.rb
r6221 r6899 2 2 module Helpers #:nodoc: 3 3 HELPERS_DIR = (defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/app/helpers" : "app/helpers") 4 4 5 5 def self.included(base) 6 6 # Initialize the base module to aggregate its helpers. … … 20 20 21 21 # 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 23 23 # Active Records (ActiveRecordHelper) that's available to all templates by default. 24 24 # 25 25 # 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 27 27 # (often the common helpers) as they're used by the specific application. 28 # 28 # 29 29 # module MyHelper 30 30 # def hello_world() "hello world" end 31 31 # end 32 # 32 # 33 33 # MyHelper can now be included in a controller, like this: 34 # 34 # 35 35 # class MyController < ActionController::Base 36 36 # helper :my_helper 37 37 # end 38 # 38 # 39 39 # ...and, same as above, used in any template rendered from MyController, like this: 40 # 40 # 41 41 # Let's hear what the helper has to say: <tt><%= hello_world %></tt> 42 42 module ClassMethods 43 43 # 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 45 45 # available to the templates. 46 46 def add_template_helper(helper_module) #:nodoc: … … 74 74 file_name = arg.to_s.underscore + '_helper' 75 75 class_name = file_name.camelize 76 76 77 77 begin 78 78 require_dependency(file_name) … … 92 92 master_helper_module.module_eval(&block) if block_given? 93 93 end 94 94 95 95 # Declare a controller method as a helper. For example, 96 96 # helper_method :link_to … … 117 117 118 118 119 private 119 private 120 120 def default_helper_module! 121 121 module_name = name.sub(/Controller$|$/, 'Helper') … … 142 142 end 143 143 end 144 144 145 # Extract helper names from files in app/helpers/**/*.rb 145 146 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' } 150 149 end 151 150 end