From 0fa0107d2d87f751c6768fa8a0991149134de2df Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 4 Feb 2019 13:21:18 -0800 Subject: Take in to account optional arguments when deprecating Refs: rails/jbuilder#452 --- actionview/lib/action_view/template/handlers.rb | 2 +- actionview/test/template/render_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'actionview') diff --git a/actionview/lib/action_view/template/handlers.rb b/actionview/lib/action_view/template/handlers.rb index f2a2341b8e..ddaac7a100 100644 --- a/actionview/lib/action_view/template/handlers.rb +++ b/actionview/lib/action_view/template/handlers.rb @@ -43,7 +43,7 @@ module ActionView #:nodoc: handler.method(:call).parameters end - unless params.find_all { |type, _| type == :req }.length >= 2 + unless params.find_all { |type, _| type == :req || type == :opt }.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. diff --git a/actionview/test/template/render_test.rb b/actionview/test/template/render_test.rb index f5d251da20..f89d087c34 100644 --- a/actionview/test/template/render_test.rb +++ b/actionview/test/template/render_test.rb @@ -471,6 +471,15 @@ module RenderTestCases ActionView::Template.unregister_template_handler :ruby_handler end + def test_optional_second_arg_works_without_deprecation + assert_not_deprecated do + ActionView::Template.register_template_handler :ruby_handler, ->(view, source = nil) { source } + end + assert_equal "3", @view.render(inline: "(1 + 2).to_s", type: :ruby_handler) + ensure + ActionView::Template.unregister_template_handler :ruby_handler + end + def test_render_inline_with_compilable_custom_type ActionView::Template.register_template_handler :foo, CustomHandler assert_equal 'source: "Hello, World!"', @view.render(inline: "Hello, World!", type: :foo) -- cgit v1.2.3