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: NAaron Patterson <tenderlove@github.com>
上级 4fbb1e9f
......@@ -10,9 +10,6 @@ def initialize(title:)
@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 @@ def render_in(view_context)
rendered_template
end
def format
:html
end
def self.template
<<~'erb'
<span title="<%= title %>">(<%= render(plain: "Inline render") %>)</span>
......
......@@ -25,6 +25,9 @@ def render(context, options)
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
......
......@@ -144,6 +144,8 @@ def _normalize_args(action = nil, options = {})
else
if action.respond_to?(:permitted?) && action.permitted?
options = action
elsif action.respond_to?(:render_in)
options[:object] = action
else
options[:partial] = action
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册