Include layout when rendering objects from controllers
In https://github.com/rails/rails/pull/37919, support for rendering objects that respond_to render_in in controllers was added. However, the implementation did not support layouts. This change updates the implementation from #37919 to more closely match the rest of the ActionView::Template classes, enabling the use of layouts. Co-authored-by: Felipe Sateler <fsateler@gmail.com>
This commit is contained in:
parent
2e8ca04906
commit
136b814144
|
@ -1,6 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "abstract_unit"
|
require "abstract_unit"
|
||||||
|
require "test_renderable"
|
||||||
|
|
||||||
module ControllerLayouts
|
module ControllerLayouts
|
||||||
class ImplicitController < ::ApplicationController
|
class ImplicitController < ::ApplicationController
|
||||||
|
@ -19,6 +20,10 @@ module ControllerLayouts
|
||||||
render template: "basic", layout: "override"
|
render template: "basic", layout: "override"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def override_renderable
|
||||||
|
render TestRenderable.new, layout: "override"
|
||||||
|
end
|
||||||
|
|
||||||
def layout_false
|
def layout_false
|
||||||
render layout: false
|
render layout: false
|
||||||
end
|
end
|
||||||
|
@ -36,6 +41,10 @@ module ControllerLayouts
|
||||||
def index
|
def index
|
||||||
render template: "basic"
|
render template: "basic"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def renderable
|
||||||
|
render TestRenderable.new
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class RenderLayoutTest < Rack::TestCase
|
class RenderLayoutTest < Rack::TestCase
|
||||||
|
@ -53,6 +62,20 @@ module ControllerLayouts
|
||||||
assert_status 200
|
assert_status 200
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "rendering a renderable object, using the implicit layout" do
|
||||||
|
get "/controller_layouts/implicit_name/renderable"
|
||||||
|
|
||||||
|
assert_body "Implicit Hello, World! Layout"
|
||||||
|
assert_status 200
|
||||||
|
end
|
||||||
|
|
||||||
|
test "rendering a renderable object, using the override layout" do
|
||||||
|
get "/controller_layouts/implicit/override_renderable"
|
||||||
|
|
||||||
|
assert_body "Override! Hello, World!"
|
||||||
|
assert_status 200
|
||||||
|
end
|
||||||
|
|
||||||
test "overriding an implicit layout with render :layout option" do
|
test "overriding an implicit layout with render :layout option" do
|
||||||
get "/controller_layouts/implicit/override"
|
get "/controller_layouts/implicit/override"
|
||||||
assert_body "Override! Hello world!"
|
assert_body "Override! Hello world!"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "abstract_unit"
|
require "abstract_unit"
|
||||||
require "test_component"
|
require "test_renderable"
|
||||||
|
|
||||||
class RendererTest < ActiveSupport::TestCase
|
class RendererTest < ActiveSupport::TestCase
|
||||||
test "action controller base has a renderer" do
|
test "action controller base has a renderer" do
|
||||||
|
@ -66,12 +66,12 @@ 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
|
test "render a renderable object" do
|
||||||
renderer = ApplicationController.renderer
|
renderer = ApplicationController.renderer
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
%(Hello, World!),
|
%(Hello, World!),
|
||||||
renderer.render(TestComponent.new)
|
renderer.render(TestRenderable.new)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class TestComponent
|
class TestRenderable
|
||||||
def render_in(_view_context)
|
def render_in(_view_context)
|
||||||
"Hello, World!"
|
"Hello, World!"
|
||||||
end
|
end
|
|
@ -25,9 +25,6 @@ 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
|
||||||
|
|
|
@ -37,6 +37,8 @@ module ActionView
|
||||||
@lookup_context.formats.first
|
@lookup_context.formats.first
|
||||||
end
|
end
|
||||||
Template::Inline.new(options[:inline], "inline template", handler, locals: keys, format: format)
|
Template::Inline.new(options[:inline], "inline template", handler, locals: keys, format: format)
|
||||||
|
elsif options.key?(:renderable)
|
||||||
|
Template::Renderable.new(options[:renderable])
|
||||||
elsif options.key?(:template)
|
elsif options.key?(:template)
|
||||||
if options[:template].respond_to?(:render)
|
if options[:template].respond_to?(:render)
|
||||||
options[:template]
|
options[:template]
|
||||||
|
|
|
@ -145,7 +145,7 @@ module ActionView
|
||||||
if action.respond_to?(:permitted?) && action.permitted?
|
if action.respond_to?(:permitted?) && action.permitted?
|
||||||
options = action
|
options = action
|
||||||
elsif action.respond_to?(:render_in)
|
elsif action.respond_to?(:render_in)
|
||||||
options[:object] = action
|
options[:renderable] = action
|
||||||
else
|
else
|
||||||
options[:partial] = action
|
options[:partial] = action
|
||||||
end
|
end
|
||||||
|
|
|
@ -111,6 +111,7 @@ module ActionView
|
||||||
eager_autoload do
|
eager_autoload do
|
||||||
autoload :Error
|
autoload :Error
|
||||||
autoload :RawFile
|
autoload :RawFile
|
||||||
|
autoload :Renderable
|
||||||
autoload :Handlers
|
autoload :Handlers
|
||||||
autoload :HTML
|
autoload :HTML
|
||||||
autoload :Inline
|
autoload :Inline
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module ActionView
|
||||||
|
# = Action View Renderable Template for objects that respond to #render_in
|
||||||
|
class Template
|
||||||
|
class Renderable # :nodoc:
|
||||||
|
def initialize(renderable)
|
||||||
|
@renderable = renderable
|
||||||
|
end
|
||||||
|
|
||||||
|
def identifier
|
||||||
|
@renderable.class.name
|
||||||
|
end
|
||||||
|
|
||||||
|
def render(context, *args)
|
||||||
|
@renderable.render_in(context)
|
||||||
|
end
|
||||||
|
|
||||||
|
def format
|
||||||
|
@renderable.format
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Binary file not shown.
|
@ -1,6 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class TestComponent
|
class TestRenderable
|
||||||
def render_in(_view_context)
|
def render_in(_view_context)
|
||||||
"Hello, World!"
|
"Hello, World!"
|
||||||
end
|
end
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
require "abstract_unit"
|
require "abstract_unit"
|
||||||
require "controller/fake_models"
|
require "controller/fake_models"
|
||||||
require "test_component"
|
require "test_renderable"
|
||||||
require "active_model/validations"
|
require "active_model/validations"
|
||||||
|
|
||||||
class TestController < ActionController::Base
|
class TestController < ActionController::Base
|
||||||
|
@ -331,7 +331,7 @@ module RenderTestCases
|
||||||
assert_match(/`undefined' for #<ActionView::Base:0x[0-9a-f]+>/, e.message)
|
assert_match(/`undefined' for #<ActionView::Base:0x[0-9a-f]+>/, e.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_object
|
def test_render_renderable_object
|
||||||
assert_equal "Hello: david", @view.render(partial: "test/customer", object: Customer.new("david"))
|
assert_equal "Hello: david", @view.render(partial: "test/customer", object: Customer.new("david"))
|
||||||
assert_equal "FalseClass", @view.render(partial: "test/klass", object: false)
|
assert_equal "FalseClass", @view.render(partial: "test/klass", object: false)
|
||||||
assert_equal "NilClass", @view.render(partial: "test/klass", object: nil)
|
assert_equal "NilClass", @view.render(partial: "test/klass", object: nil)
|
||||||
|
@ -697,10 +697,10 @@ module RenderTestCases
|
||||||
assert_raises(ArgumentError) { ActionView::Template.register_template_handler CustomHandler }
|
assert_raises(ArgumentError) { ActionView::Template.register_template_handler CustomHandler }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_component
|
def test_render_object
|
||||||
assert_equal(
|
assert_equal(
|
||||||
%(Hello, World!),
|
%(Hello, World!),
|
||||||
@view.render(TestComponent.new)
|
@view.render(TestRenderable.new)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -282,7 +282,7 @@ TIP: `send_file` is often a faster and better option if a layout isn't required.
|
||||||
Rails can render objects responding to `:render_in`.
|
Rails can render objects responding to `:render_in`.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render MyComponent.new
|
render MyRenderable.new
|
||||||
```
|
```
|
||||||
|
|
||||||
This calls `render_in` on the provided object with the current view context.
|
This calls `render_in` on the provided object with the current view context.
|
||||||
|
|
Loading…
Reference in New Issue