From 20a83f52d7476b8a22e139fd29d8f4b014578f9a Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Mon, 9 Dec 2019 15:43:20 -0700 Subject: [PATCH] Render objects that respond_to render_in in controllers In https://github.com/rails/rails/pull/36388, we supported passing objects that `respond_to` `render_in` to `render`, but _only_ in views. This change does the same for controllers, as Rails generally gives the expectation that `render` behaves the same in both contexts. Co-authored-by: Aaron Patterson --- actionpack/test/lib/test_component.rb | 7 ++++--- actionview/lib/action_view/renderer/renderer.rb | 3 +++ actionview/lib/action_view/rendering.rb | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/actionpack/test/lib/test_component.rb b/actionpack/test/lib/test_component.rb index b45b530efd..4bfc11ac60 100644 --- a/actionpack/test/lib/test_component.rb +++ b/actionpack/test/lib/test_component.rb @@ -10,9 +10,6 @@ class TestComponent < ActionView::Base @title = title end - # Entrypoint for rendering. Called by ActionView::RenderingHelper#render. - # - # Returns ActionView::OutputBuffer. def render_in(view_context) self.class.compile @view_context = view_context @@ -20,6 +17,10 @@ class TestComponent < ActionView::Base rendered_template end + def format + :html + end + def self.template <<~'erb' (<%= render(plain: "Inline render") %>) diff --git a/actionview/lib/action_view/renderer/renderer.rb b/actionview/lib/action_view/renderer/renderer.rb index 485eb1a5b4..75f235fe63 100644 --- a/actionview/lib/action_view/renderer/renderer.rb +++ b/actionview/lib/action_view/renderer/renderer.rb @@ -25,6 +25,9 @@ module ActionView def render_to_object(context, options) # :nodoc: if options.key?(:partial) render_partial_to_object(context, options) + elsif options.key?(:object) + object = options[:object] + AbstractRenderer::RenderedTemplate.new(object.render_in(context), object) else render_template_to_object(context, options) end diff --git a/actionview/lib/action_view/rendering.rb b/actionview/lib/action_view/rendering.rb index 7247598ac2..9ee6dabf31 100644 --- a/actionview/lib/action_view/rendering.rb +++ b/actionview/lib/action_view/rendering.rb @@ -144,6 +144,8 @@ module ActionView else if action.respond_to?(:permitted?) && action.permitted? options = action + elsif action.respond_to?(:render_in) + options[:object] = action else options[:partial] = action end