Merge pull request #37919 from joelhawksley/controller-render-in

Render objects that respond_to render_in in controllers
This commit is contained in:
Eileen M. Uchitelle 2019-12-10 09:18:26 -05:00 committed by GitHub
commit e36097a950
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 0 deletions

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_unit" require "abstract_unit"
require "test_component"
class RendererTest < ActiveSupport::TestCase class RendererTest < ActiveSupport::TestCase
test "action controller base has a renderer" do test "action controller base has a renderer" do
@ -65,6 +66,15 @@ class RendererTest < ActiveSupport::TestCase
assert_equal "The secret is foo\n", content assert_equal "The secret is foo\n", content
end end
def test_render_component
renderer = ApplicationController.renderer
assert_equal(
%(<span title="my title">(Inline render)</span>),
renderer.render(TestComponent.new(title: "my title")).strip
)
end
test "rendering with custom env" do test "rendering with custom env" do
renderer = ApplicationController.renderer.new method: "post" renderer = ApplicationController.renderer.new method: "post"
content = renderer.render inline: "<%= request.post? %>" content = renderer.render inline: "<%= request.post? %>"

View File

@ -0,0 +1,45 @@
# frozen_string_literal: true
class TestComponent < ActionView::Base
include ActiveModel::Validations
validates :title, presence: true
delegate :render, to: :view_context
def initialize(title:)
@title = title
end
def render_in(view_context)
self.class.compile
@view_context = view_context
validate!
rendered_template
end
def format
:html
end
def self.template
<<~'erb'
<span title="<%= title %>">(<%= render(plain: "Inline render") %>)</span>
erb
end
def self.compile
@compiled ||= nil
return if @compiled
class_eval(
"def rendered_template; @output_buffer = ActionView::OutputBuffer.new; " +
ActionView::Template::Handlers::ERB.erb_implementation.new(template, trim: true).src +
"; end"
)
@compiled = true
end
private
attr_reader :title, :view_context
end

View File

@ -25,6 +25,9 @@ module ActionView
def render_to_object(context, options) # :nodoc: def render_to_object(context, options) # :nodoc:
if options.key?(:partial) if options.key?(:partial)
render_partial_to_object(context, options) render_partial_to_object(context, options)
elsif options.key?(:object)
object = options[:object]
AbstractRenderer::RenderedTemplate.new(object.render_in(context), object)
else else
render_template_to_object(context, options) render_template_to_object(context, options)
end end

View File

@ -144,6 +144,8 @@ module ActionView
else else
if action.respond_to?(:permitted?) && action.permitted? if action.respond_to?(:permitted?) && action.permitted?
options = action options = action
elsif action.respond_to?(:render_in)
options[:object] = action
else else
options[:partial] = action options[:partial] = action
end end