2009-06-15 14:44:45 -04:00
|
|
|
module ActionController
|
2010-08-26 16:32:40 -04:00
|
|
|
# The \Rails framework provides a large number of helpers for working with assets, dates, forms,
|
|
|
|
# numbers and model objects, to name a few. These helpers are available to all templates
|
2009-06-15 14:44:45 -04:00
|
|
|
# by default.
|
|
|
|
#
|
2010-08-26 16:32:40 -04:00
|
|
|
# In addition to using the standard template helpers provided, creating custom helpers to
|
2011-09-03 15:13:40 -04:00
|
|
|
# extract complicated logic or reusable functionality is strongly encouraged. By default, each controller
|
2013-07-30 14:30:10 -04:00
|
|
|
# will include all helpers. These helpers are only accessible on the controller through <tt>.helpers</tt>
|
2011-09-03 15:13:40 -04:00
|
|
|
#
|
|
|
|
# In previous versions of \Rails the controller will include a helper whose
|
|
|
|
# name matches that of the controller, e.g., <tt>MyController</tt> will automatically
|
|
|
|
# include <tt>MyHelper</tt>. To return old behavior set +config.action_controller.include_all_helpers+ to +false+.
|
2009-06-15 14:44:45 -04:00
|
|
|
#
|
2010-08-26 16:32:40 -04:00
|
|
|
# Additional helpers can be specified using the +helper+ class method in ActionController::Base or any
|
2009-06-15 14:44:45 -04:00
|
|
|
# controller which inherits from it.
|
|
|
|
#
|
2010-08-26 16:32:40 -04:00
|
|
|
# The +to_s+ method from the \Time class can be wrapped in a helper method to display a custom message if
|
|
|
|
# a \Time object is blank:
|
2009-06-15 14:44:45 -04:00
|
|
|
#
|
|
|
|
# module FormattedTimeHelper
|
|
|
|
# def format_time(time, format=:long, blank_message=" ")
|
|
|
|
# time.blank? ? blank_message : time.to_s(format)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# FormattedTimeHelper can now be included in a controller, using the +helper+ class method:
|
|
|
|
#
|
|
|
|
# class EventsController < ActionController::Base
|
|
|
|
# helper FormattedTimeHelper
|
|
|
|
# def index
|
2011-07-07 12:12:09 -04:00
|
|
|
# @events = Event.all
|
2009-06-15 14:44:45 -04:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# Then, in any view rendered by <tt>EventController</tt>, the <tt>format_time</tt> method can be called:
|
|
|
|
#
|
|
|
|
# <% @events.each do |event| -%>
|
|
|
|
# <p>
|
2010-04-05 05:51:26 -04:00
|
|
|
# <%= format_time(event.time, :short, "N/A") %> | <%= event.name %>
|
2009-06-15 14:44:45 -04:00
|
|
|
# </p>
|
|
|
|
# <% end -%>
|
|
|
|
#
|
|
|
|
# Finally, assuming we have two event instances, one which has a time and one which does not,
|
|
|
|
# the output might look like this:
|
|
|
|
#
|
|
|
|
# 23 Aug 11:30 | Carolina Railhawks Soccer Match
|
|
|
|
# N/A | Carolina Railhaws Training Workshop
|
|
|
|
#
|
|
|
|
module Helpers
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-22 18:17:05 -04:00
|
|
|
|
2012-12-13 06:36:58 -05:00
|
|
|
class << self; attr_accessor :helpers_path; end
|
2009-06-15 14:44:45 -04:00
|
|
|
include AbstractController::Helpers
|
2005-06-15 13:17:58 -04:00
|
|
|
|
2009-06-15 14:44:45 -04:00
|
|
|
included do
|
2011-12-24 03:59:28 -05:00
|
|
|
class_attribute :helpers_path, :include_all_helpers
|
2010-04-22 06:00:13 -04:00
|
|
|
self.helpers_path ||= []
|
2010-11-17 18:06:03 -05:00
|
|
|
self.include_all_helpers = true
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
2007-08-31 15:07:42 -04:00
|
|
|
# Declares helper accessors for controller attributes. For example, the
|
|
|
|
# following adds new +name+ and <tt>name=</tt> instance methods to a
|
|
|
|
# controller and makes them available to the view:
|
2004-11-23 20:04:44 -05:00
|
|
|
# attr_accessor :name
|
2010-08-26 16:32:40 -04:00
|
|
|
# helper_attr :name
|
2009-06-15 14:44:45 -04:00
|
|
|
#
|
|
|
|
# ==== Parameters
|
2010-08-26 16:32:40 -04:00
|
|
|
# * <tt>attrs</tt> - Names of attributes to be converted into helpers.
|
2004-11-23 20:04:44 -05:00
|
|
|
def helper_attr(*attrs)
|
|
|
|
attrs.flatten.each { |attr| helper_method(attr, "#{attr}=") }
|
|
|
|
end
|
|
|
|
|
2008-04-06 14:42:34 -04:00
|
|
|
# Provides a proxy to access helpers methods from outside the view.
|
|
|
|
def helpers
|
2013-10-24 00:54:20 -04:00
|
|
|
@helper_proxy ||= begin
|
|
|
|
proxy = ActionView::Base.new
|
|
|
|
proxy.config = config.inheritable_copy
|
|
|
|
proxy.extend(_helpers)
|
|
|
|
end
|
2008-04-06 14:42:34 -04:00
|
|
|
end
|
2007-02-24 15:31:34 -05:00
|
|
|
|
2011-04-25 09:02:41 -04:00
|
|
|
# Overwrite modules_for_helpers to accept :all as argument, which loads
|
|
|
|
# all helpers in helpers_path.
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# * <tt>args</tt> - A list of helpers
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# * <tt>array</tt> - A normalized list of modules for the list of helpers provided.
|
|
|
|
def modules_for_helpers(args)
|
|
|
|
args += all_application_helpers if args.delete(:all)
|
|
|
|
super(args)
|
|
|
|
end
|
2005-11-08 03:23:13 -05:00
|
|
|
|
2015-03-03 15:27:17 -05:00
|
|
|
# Returns a list of helper names in a given path.
|
2015-03-02 17:12:30 -05:00
|
|
|
#
|
|
|
|
# ActionController::Base.all_helpers_from_path 'app/helpers'
|
|
|
|
# # => ["application", "chart", "rubygems"]
|
2011-04-25 09:02:41 -04:00
|
|
|
def all_helpers_from_path(path)
|
2012-12-13 05:46:12 -05:00
|
|
|
helpers = Array(path).flat_map do |_path|
|
|
|
|
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_helper.rb$/
|
2012-05-27 08:01:44 -04:00
|
|
|
names = Dir["#{_path}/**/*_helper.rb"].map { |file| file.sub(extract, '\1') }
|
2012-12-13 05:46:12 -05:00
|
|
|
names.sort!
|
2010-07-30 01:14:48 -04:00
|
|
|
end
|
2011-04-25 09:02:41 -04:00
|
|
|
helpers.uniq!
|
|
|
|
helpers
|
|
|
|
end
|
2010-07-30 01:14:48 -04:00
|
|
|
|
2011-04-25 09:02:41 -04:00
|
|
|
private
|
|
|
|
# Extract helper names from files in <tt>app/helpers/**/*_helper.rb</tt>
|
|
|
|
def all_application_helpers
|
|
|
|
all_helpers_from_path(helpers_path)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|