2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-01 19:10:02 -05:00
|
|
|
require "active_support/deprecation"
|
|
|
|
|
2008-06-11 23:22:05 -04:00
|
|
|
module ActionView #:nodoc:
|
2010-06-20 16:26:31 -04:00
|
|
|
# = Action View Template Handlers
|
2017-08-26 20:12:19 -04:00
|
|
|
class Template #:nodoc:
|
2009-12-02 23:01:01 -05:00
|
|
|
module Handlers #:nodoc:
|
2016-08-06 12:48:35 -04:00
|
|
|
autoload :Raw, "action_view/template/handlers/raw"
|
|
|
|
autoload :ERB, "action_view/template/handlers/erb"
|
|
|
|
autoload :Html, "action_view/template/handlers/html"
|
|
|
|
autoload :Builder, "action_view/template/handlers/builder"
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
def self.extended(base)
|
2015-01-03 16:00:27 -05:00
|
|
|
base.register_default_template_handler :raw, Raw.new
|
|
|
|
base.register_template_handler :erb, ERB.new
|
2016-01-05 14:48:09 -05:00
|
|
|
base.register_template_handler :html, Html.new
|
2010-10-09 19:14:50 -04:00
|
|
|
base.register_template_handler :builder, Builder.new
|
2019-02-01 19:10:02 -05:00
|
|
|
base.register_template_handler :ruby, lambda { |_, source| source }
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@@template_handlers = {}
|
|
|
|
@@default_template_handlers = nil
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def self.extensions
|
2010-06-04 20:28:04 -04:00
|
|
|
@@template_extensions ||= @@template_handlers.keys
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2019-02-01 19:10:02 -05:00
|
|
|
class LegacyHandlerWrapper < SimpleDelegator # :nodoc:
|
|
|
|
def call(view, source)
|
|
|
|
__getobj__.call(ActionView::Template::LegacyTemplate.new(view, source))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-01 19:49:21 -05:00
|
|
|
# Register an object that knows how to handle template files with the given
|
2012-09-14 11:25:07 -04:00
|
|
|
# extensions. This can be used to implement new template types.
|
2014-11-20 17:54:22 -05:00
|
|
|
# The handler must respond to +:call+, which will be passed the template
|
2012-02-01 19:49:21 -05:00
|
|
|
# and should return the rendered template as a String.
|
2012-09-14 11:25:07 -04:00
|
|
|
def register_template_handler(*extensions, handler)
|
2019-02-01 19:10:02 -05:00
|
|
|
params = if handler.is_a?(Proc)
|
|
|
|
handler.parameters
|
|
|
|
else
|
|
|
|
handler.method(:call).parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
unless params.find_all { |type, _| type == :req }.length >= 2
|
|
|
|
ActiveSupport::Deprecation.warn <<~eowarn
|
|
|
|
Single arity template handlers are deprecated. Template handlers must
|
|
|
|
now accept two parameters, the view object and the source for the view object.
|
|
|
|
Change:
|
|
|
|
>> #{handler.class}#call(#{params.map(&:last).join(", ")})
|
|
|
|
To:
|
|
|
|
>> #{handler.class}#call(#{params.map(&:last).join(", ")}, source)
|
|
|
|
eowarn
|
|
|
|
handler = LegacyHandlerWrapper.new(handler)
|
|
|
|
end
|
|
|
|
|
2012-09-14 11:25:07 -04:00
|
|
|
raise(ArgumentError, "Extension is required") if extensions.empty?
|
|
|
|
extensions.each do |extension|
|
|
|
|
@@template_handlers[extension.to_sym] = handler
|
|
|
|
end
|
2012-02-20 14:48:14 -05:00
|
|
|
@@template_extensions = nil
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2014-06-09 10:45:46 -04:00
|
|
|
# Opposite to register_template_handler.
|
|
|
|
def unregister_template_handler(*extensions)
|
|
|
|
extensions.each do |extension|
|
|
|
|
handler = @@template_handlers.delete extension.to_sym
|
|
|
|
@@default_template_handlers = nil if @@default_template_handlers == handler
|
|
|
|
end
|
|
|
|
@@template_extensions = nil
|
|
|
|
end
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def template_handler_extensions
|
2014-10-27 12:28:53 -04:00
|
|
|
@@template_handlers.keys.map(&:to_s).sort
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def registered_template_handler(extension)
|
|
|
|
extension && @@template_handlers[extension.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_default_template_handler(extension, klass)
|
|
|
|
register_template_handler(extension, klass)
|
|
|
|
@@default_template_handlers = klass
|
|
|
|
end
|
|
|
|
|
2010-12-01 05:22:48 -05:00
|
|
|
def handler_for_extension(extension)
|
|
|
|
registered_template_handler(extension) || @@default_template_handlers
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
2008-06-11 23:22:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|