2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
2016-05-05 13:02:28 -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
|
|
|
#
|
2015-09-01 10:37:37 -04:00
|
|
|
# In previous versions of \Rails the controller will include a helper which
|
|
|
|
# matches the name of the controller, e.g., <tt>MyController</tt> will automatically
|
2011-09-03 15:13:40 -04:00
|
|
|
# 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
|
|
|
|
#
|
2019-03-12 11:52:09 -04:00
|
|
|
# Then, in any view rendered by <tt>EventsController</tt>, the <tt>format_time</tt> method can be called:
|
2009-06-15 14:44:45 -04:00
|
|
|
#
|
|
|
|
# <% @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
|
2015-05-20 13:22:18 -04:00
|
|
|
# N/A | Carolina Railhawks Training Workshop
|
2009-06-15 14:44:45 -04:00
|
|
|
#
|
|
|
|
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
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :helpers_path, default: []
|
|
|
|
class_attribute :include_all_helpers, default: 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
|
|
|
|
|
2016-05-07 11:15:23 -04:00
|
|
|
# Provides a proxy to access helper methods from outside the view.
|
2008-04-06 14:42:34 -04:00
|
|
|
def helpers
|
2015-07-19 17:19:15 -04:00
|
|
|
@helper_proxy ||= begin
|
2019-01-29 18:17:52 -05:00
|
|
|
proxy = ActionView::Base.empty
|
2013-10-24 00:54:20 -04:00
|
|
|
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|
|
2018-08-23 06:31:30 -04:00
|
|
|
names = Dir["#{_path}/**/*_helper.rb"].map { |file| file[_path.to_s.size + 1..-"_helper.rb".size - 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
|
2016-09-14 04:57:52 -04:00
|
|
|
# Extract helper names from files in <tt>app/helpers/**/*_helper.rb</tt>
|
2016-08-06 13:55:02 -04:00
|
|
|
def all_application_helpers
|
|
|
|
all_helpers_from_path(helpers_path)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2016-05-05 13:02:28 -04:00
|
|
|
|
2016-05-07 11:15:23 -04:00
|
|
|
# Provides a proxy to access helper methods from outside the view.
|
2016-05-05 13:02:28 -04:00
|
|
|
def helpers
|
|
|
|
@_helper_proxy ||= view_context
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|