diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2019-02-01 16:10:02 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2019-02-01 16:19:53 -0800 |
commit | 28f88e0074f473f58c2d3fd54cb3daff81027c12 (patch) | |
tree | 17e727ed04f69bc63c1408cfc3a95a0d6063b359 /actionview/lib/action_view/template/handlers.rb | |
parent | 2169bd3d2a9d2f331a5dd6e41d9d638e0da6117c (diff) | |
download | rails-28f88e0074f473f58c2d3fd54cb3daff81027c12.tar.gz rails-28f88e0074f473f58c2d3fd54cb3daff81027c12.tar.bz2 rails-28f88e0074f473f58c2d3fd54cb3daff81027c12.zip |
Pass source to template handler and deprecate old style handler
This commit passes the mutated source to the template handler as a
parameter and deprecates the old handlers. Old handlers required that
templates contain a reference to mutated source code, but we would like
to make template objects "read only". This change lets the template
remain "read only" while still giving template handlers access to the
source code after mutations.
Diffstat (limited to 'actionview/lib/action_view/template/handlers.rb')
-rw-r--r-- | actionview/lib/action_view/template/handlers.rb | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/actionview/lib/action_view/template/handlers.rb b/actionview/lib/action_view/template/handlers.rb index 7ec76dcc3f..f2a2341b8e 100644 --- a/actionview/lib/action_view/template/handlers.rb +++ b/actionview/lib/action_view/template/handlers.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_support/deprecation" + module ActionView #:nodoc: # = Action View Template Handlers class Template #:nodoc: @@ -14,7 +16,7 @@ module ActionView #:nodoc: base.register_template_handler :erb, ERB.new base.register_template_handler :html, Html.new base.register_template_handler :builder, Builder.new - base.register_template_handler :ruby, :source.to_proc + base.register_template_handler :ruby, lambda { |_, source| source } end @@template_handlers = {} @@ -24,11 +26,35 @@ module ActionView #:nodoc: @@template_extensions ||= @@template_handlers.keys end + class LegacyHandlerWrapper < SimpleDelegator # :nodoc: + def call(view, source) + __getobj__.call(ActionView::Template::LegacyTemplate.new(view, source)) + end + end + # Register an object that knows how to handle template files with the given # extensions. This can be used to implement new template types. # The handler must respond to +:call+, which will be passed the template # and should return the rendered template as a String. def register_template_handler(*extensions, handler) + 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 + raise(ArgumentError, "Extension is required") if extensions.empty? extensions.each do |extension| @@template_handlers[extension.to_sym] = handler |