Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 114 lines (105 sloc) 4.021 kB
81f398b @jeremy Fix setting helpers_path to a string or pathname
jeremy authored
1 require 'active_support/core_ext/array/wrap'
e5ab4b0 @jeremy Convert to class_attribute
jeremy authored
2 require 'active_support/core_ext/class/attribute'
3
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
4 module ActionController
ca36326 @tilsammans cleanup of ActionController::Metal inline documentation
tilsammans authored
5 # The \Rails framework provides a large number of helpers for working with assets, dates, forms,
6 # numbers and model objects, to name a few. These helpers are available to all templates
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
7 # by default.
8 #
ca36326 @tilsammans cleanup of ActionController::Metal inline documentation
tilsammans authored
9 # In addition to using the standard template helpers provided, creating custom helpers to
19ec3c1 @avakhov fix helpers behavior description
avakhov authored
10 # extract complicated logic or reusable functionality is strongly encouraged. By default, each controller
11 # will include all helpers.
12 #
13 # In previous versions of \Rails the controller will include a helper whose
14 # name matches that of the controller, e.g., <tt>MyController</tt> will automatically
15 # include <tt>MyHelper</tt>. To return old behavior set +config.action_controller.include_all_helpers+ to +false+.
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
16 #
ca36326 @tilsammans cleanup of ActionController::Metal inline documentation
tilsammans authored
17 # Additional helpers can be specified using the +helper+ class method in ActionController::Base or any
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
18 # controller which inherits from it.
19 #
20 # ==== Examples
ca36326 @tilsammans cleanup of ActionController::Metal inline documentation
tilsammans authored
21 # The +to_s+ method from the \Time class can be wrapped in a helper method to display a custom message if
22 # a \Time object is blank:
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
23 #
24 # module FormattedTimeHelper
25 # def format_time(time, format=:long, blank_message="&nbsp;")
26 # time.blank? ? blank_message : time.to_s(format)
27 # end
28 # end
29 #
30 # FormattedTimeHelper can now be included in a controller, using the +helper+ class method:
31 #
32 # class EventsController < ActionController::Base
33 # helper FormattedTimeHelper
34 # def index
a3683fd @amatsuda find(:all) => all
amatsuda authored
35 # @events = Event.all
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
36 # end
37 # end
38 #
39 # Then, in any view rendered by <tt>EventController</tt>, the <tt>format_time</tt> method can be called:
40 #
41 # <% @events.each do |event| -%>
42 # <p>
9ed919e @fxn revises some <%= in rdoc
fxn authored
43 # <%= format_time(event.time, :short, "N/A") %> | <%= event.name %>
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
44 # </p>
45 # <% end -%>
46 #
47 # Finally, assuming we have two event instances, one which has a time and one which does not,
48 # the output might look like this:
49 #
50 # 23 Aug 11:30 | Carolina Railhawks Soccer Match
51 # N/A | Carolina Railhaws Training Workshop
52 #
53 module Helpers
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" …
josh authored
54 extend ActiveSupport::Concern
e976c48 @lifo Add all the existing helpers related features to the new base
lifo authored
55
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
56 include AbstractController::Helpers
7bb4860 @jeremy r1318@iwill: jeremy | 2005-06-15 01:08:22 -0700
jeremy authored
57
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
58 included do
c4ef3d3 @josevalim Remove unecessary config_accessors.
josevalim authored
59 class_attribute :helpers_path, :include_all_helpers
4163cce @josevalim Clean up the config object in ActionPack. Create config_accessor whic…
josevalim authored
60 self.helpers_path ||= []
250fb3f @drogus Add config.action_controller.include_all_helpers, by default it is se…
drogus authored
61 self.include_all_helpers = true
db045db @dhh Initial
dhh authored
62 end
63
64 module ClassMethods
ed09b62 @jeremy Documentation tweaks and fixes. Closes #9454 [sur, kampers]
jeremy authored
65 # Declares helper accessors for controller attributes. For example, the
66 # following adds new +name+ and <tt>name=</tt> instance methods to a
67 # controller and makes them available to the view:
db045db @dhh Initial
dhh authored
68 # attr_accessor :name
ca36326 @tilsammans cleanup of ActionController::Metal inline documentation
tilsammans authored
69 # helper_attr :name
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
70 #
71 # ==== Parameters
ca36326 @tilsammans cleanup of ActionController::Metal inline documentation
tilsammans authored
72 # * <tt>attrs</tt> - Names of attributes to be converted into helpers.
db045db @dhh Initial
dhh authored
73 def helper_attr(*attrs)
74 attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") }
75 end
76
917423d @josh Provide a helper proxy to access helper methods from outside views. C…
josh authored
77 # Provides a proxy to access helpers methods from outside the view.
78 def helpers
a63caa4 Get tests to run (with failures) without old base around
Yehuda Katz + Carl Lerche authored
79 @helper_proxy ||= ActionView::Base.new.extend(_helpers)
917423d @josh Provide a helper proxy to access helper methods from outside views. C…
josh authored
80 end
f95dfff @dhh Added helper(:all) as a way to include all helpers from app/helpers/*…
dhh authored
81
d4bea35 @drogus Make ActionController::Base.modules_for_helpers and ActionController:…
drogus authored
82 # Overwrite modules_for_helpers to accept :all as argument, which loads
83 # all helpers in helpers_path.
84 #
85 # ==== Parameters
86 # * <tt>args</tt> - A list of helpers
87 #
88 # ==== Returns
89 # * <tt>array</tt> - A normalized list of modules for the list of helpers provided.
90 def modules_for_helpers(args)
91 args += all_application_helpers if args.delete(:all)
92 super(args)
93 end
85baf07 @jeremy Controllers with acronyms in their names (e.g. PDFController) require…
jeremy authored
94
d4bea35 @drogus Make ActionController::Base.modules_for_helpers and ActionController:…
drogus authored
95 def all_helpers_from_path(path)
96 helpers = []
97 Array.wrap(path).each do |_path|
98 extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/
99 helpers += Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') }
c7664d1 @drogus Include application's helpers and router helpers by default, but incl…
drogus authored
100 end
d4bea35 @drogus Make ActionController::Base.modules_for_helpers and ActionController:…
drogus authored
101 helpers.sort!
102 helpers.uniq!
103 helpers
104 end
c7664d1 @drogus Include application's helpers and router helpers by default, but incl…
drogus authored
105
d4bea35 @drogus Make ActionController::Base.modules_for_helpers and ActionController:…
drogus authored
106 private
107 # Extract helper names from files in <tt>app/helpers/**/*_helper.rb</tt>
108 def all_application_helpers
109 all_helpers_from_path(helpers_path)
110 end
db045db @dhh Initial
dhh authored
111 end
112 end
0ee1cb2 @jeremy Ruby 1.9 compat, consistent load paths
jeremy authored
113 end
Something went wrong with that request. Please try again.