Skip to content

Commit 8f14f10

Browse files
committed
Pass render options and block to calls to #render_in
Closes [rails#45432][] Support for objects that respond to `#render_in` was introduced in [rails#36388][] and [rails#37919][]. Those implementations assume that the instance will all the context it needs to render itself. That assumption doesn't account for call-site arguments like `locals: { ... }` or a block. This commit expands support for rendering with a `:renderable` option to incorporate locals and blocks. For example: ```ruby class Greeting def render_in(view_context, **options, &block) if block view_context.render plain: block.call else case Array(options[:formats]).first when :json view_context.render json: { greeting: "Hello, World!" } else view_context.render **options, inline: "<%= Hello, <%= name %>!" end end end def format :html end end render(Greeting.new) # => "Hello, World!" render(Greeting.new, name: "Local") # => "Hello, Local!" render(Greeting.new) { "Hello, Block!" } # => "Hello, Block!" render(renderable: Greeting.new, formats: :json) # => "{\"greeting\":\"Hello, World!\"}" ``` Since existing tools depend on the `#render_in(view_context)` signature without options, this commit deprecates that signature in favor of one that accepts options and a block. [rails#45432]: rails#45432 [rails#36388]: rails#36388 [rails#37919]: rails#37919
1 parent 5853ff1 commit 8f14f10

File tree

18 files changed

+266
-52
lines changed

18 files changed

+266
-52
lines changed

actionpack/CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
* Accept render options and block in `render` calls made with `:renderable`
2+
3+
```ruby
4+
class Greeting
5+
def render_in(view_context, **options, &block)
6+
if block
7+
view_context.render html: block.call
8+
else
9+
view_context.render inline: <<~ERB.strip, **options
10+
Hello, <%= local_assigns.fetch(:name, "World") %>!
11+
ERB
12+
end
13+
end
14+
15+
ApplicationController.render(Greeting.new) # => "Hello, World!"
16+
ApplicationController.render(Greeting.new) { "Hello, Block!" } # => "Hello, Block!"
17+
ApplicationController.render(renderable: Greeting.new) # => "Hello, World!"
18+
ApplicationController.render(renderable: Greeting.new, locals: { name: "Local" }) # => "Hello, Local!"
19+
```
20+
21+
*Sean Doyle*
22+
123
* Add `allow_browser` to set minimum browser versions for the application.
224

325
A browser that's blocked will by default be served the file in `public/426.html` with a HTTP status code of "426 Upgrade Required".

actionpack/lib/abstract_controller/rendering.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def render(*args, &block)
4242
# needs to be overridden in order to still return a string.
4343
def render_to_string(*args, &block)
4444
options = _normalize_render(*args, &block)
45-
render_to_body(options)
45+
render_to_body(options, &block)
4646
end
4747

4848
# Performs the actual template rendering.
49-
def render_to_body(options = {})
49+
def render_to_body(options = {}, &block)
5050
end
5151

5252
# Returns +Content-Type+ of rendered content.

actionpack/lib/action_controller/metal/rendering.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,31 @@ def inherited(klass)
3737
# # => renders app/views/posts/show.html.erb
3838
#
3939
# If the first argument responds to +render_in+, the template will be
40-
# rendered by calling +render_in+ with the current view context.
40+
# rendered by calling +render_in+ with the current view context, render
41+
# options, and block.
4142
#
4243
# class Greeting
43-
# def render_in(view_context)
44-
# view_context.render html: "<h1>Hello, World</h1>"
44+
# def render_in(view_context, **options, &block)
45+
# if block
46+
# view_context.render html: block.call
47+
# else
48+
# view_context.render inline: <<~ERB.strip, **options
49+
# <h1><%= Hello, <%= local_assigns.fetch(:name, "World") %></h1>
50+
# ERB
51+
# end
4552
# end
4653
#
4754
# def format
4855
# :html
4956
# end
5057
# end
5158
#
52-
# render(Greeting.new)
53-
# # => "<h1>Hello, World</h1>"
54-
#
55-
# render(renderable: Greeting.new)
56-
# # => "<h1>Hello, World</h1>"
59+
# render(Greeting.new) # => "<h1>Hello, World</h1>"
60+
# render(renderable: Greeting.new) # => "<h1>Hello, World</h1>"
61+
# render(Greeting.new, name: "Local") # => "<h1>Hello, Local</h1>"
62+
# render(renderable: Greeting.new, locals: { name: "Local" }) # => "<h1>Hello, Local</h1>"
63+
# render(Greeting.new) { "<h1>Hello, Block</h1>" } # => "<h1>Hello, Block</h1>"
64+
# render(renderable: Greeting.new) { "<h1>Hello, Block<h1>" } # => "<h1>Hello, Block</h1>"
5765
#
5866
# ==== \Rendering Mode
5967
#
@@ -110,12 +118,16 @@ def inherited(klass)
110118
#
111119
# [+:renderable+]
112120
# Renders the provided object by calling +render_in+ with the current view
113-
# context. The response format is determined by calling +format+ on the
114-
# renderable if it responds to +format+, falling back to +text/html+ by default.
121+
# context, render options, and block. The response format is determined by
122+
# calling +format+ on the renderable if it responds to +format+, falling
123+
# back to +text/html+ by default.
115124
#
116125
# render renderable: Greeting.new
117126
# # => renders "<h1>Hello, World</h1>"
118127
#
128+
# render renderable: Greeting.new, locals: { name: "Local" }
129+
# # => renders "<h1>Hello, Local</h1>"
130+
#
119131
# By default, when a rendering mode is specified, no layout template is
120132
# rendered.
121133
#

actionpack/lib/action_controller/renderer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ def defaults
120120
end
121121

122122
# Renders a template to a string, just like ActionController::Rendering#render_to_string.
123-
def render(*args)
123+
def render(...)
124124
request = ActionDispatch::Request.new(env_for_request)
125125
request.routes = controller._routes
126126

127127
instance = controller.new
128128
instance.set_request! request
129129
instance.set_response! controller.make_response!(request)
130-
instance.render_to_string(*args)
130+
instance.render_to_string(...)
131131
end
132132
alias_method :render_to_string, :render # :nodoc:
133133

actionpack/test/controller/render_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "abstract_unit"
44
require "controller/fake_models"
5+
require "test_renderable"
56

67
class TestControllerWithExtraEtags < ActionController::Base
78
self.view_paths = [ActionView::FixtureResolver.new(
@@ -363,6 +364,10 @@ class MetalTestController < ActionController::Metal
363364
def accessing_logger_in_template
364365
render inline: "<%= logger.class %>"
365366
end
367+
368+
def render_renderable
369+
render renderable: TestRenderable.new, locals: params.fetch(:locals, {})
370+
end
366371
end
367372

368373
class ExpiresInRenderTest < ActionController::TestCase
@@ -790,6 +795,16 @@ def test_access_to_logger_in_view
790795
get :accessing_logger_in_template
791796
assert_equal "NilClass", @response.body
792797
end
798+
799+
def test_render_renderable
800+
get :render_renderable
801+
802+
assert_equal "Hello, World!", @response.parsed_body.text
803+
804+
get :render_renderable, params: { locals: { name: "Local" } }
805+
806+
assert_equal "Hello, Local!", @response.parsed_body.text
807+
end
793808
end
794809

795810
class ActionControllerRenderTest < ActionController::TestCase

actionpack/test/controller/renderer_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ class RendererTest < ActiveSupport::TestCase
7777
%(Hello, World!),
7878
renderer.render(renderable: TestRenderable.new)
7979
)
80+
assert_equal(
81+
%(Hello, Local!),
82+
renderer.render(TestRenderable.new, name: "Local")
83+
)
84+
assert_equal(
85+
%(Hello, Local!),
86+
renderer.render(renderable: TestRenderable.new, locals: { name: "Local" })
87+
)
88+
end
89+
90+
test "render a renderable object with block" do
91+
renderer = ApplicationController.renderer
92+
93+
assert_equal(
94+
%(<h1>Goodbye, World!</h1>),
95+
renderer.render(TestRenderable.new) { "<h1>Goodbye, World!</h1>".html_safe }
96+
)
97+
assert_equal(
98+
%(<h1>Goodbye, World!</h1>),
99+
renderer.render(renderable: TestRenderable.new) { "<h1>Goodbye, World!</h1>".html_safe }
100+
)
80101
end
81102

82103
test "rendering with custom env" do

actionpack/test/lib/test_renderable.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# frozen_string_literal: true
22

33
class TestRenderable
4-
def render_in(_view_context)
5-
"Hello, World!"
4+
def render_in(view_context, locals: {}, **options, &block)
5+
if block
6+
view_context.render html: block.call
7+
else
8+
view_context.render inline: <<~ERB.strip, locals: locals
9+
Hello, <%= local_assigns.fetch(:name, "World") %>!
10+
ERB
11+
end
612
end
713

814
def format

actionview/CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
* Pass render options and block to calls to `#render_in`
2+
3+
```ruby
4+
class Greeting
5+
def render_in(view_context, **options, &block)
6+
if block
7+
view_context.render html: block.call
8+
else
9+
view_context.render inline: <<~ERB.strip, **options
10+
Hello, <%= local_assigns.fetch(:name, "World") %>!
11+
ERB
12+
end
13+
end
14+
end
15+
16+
render(Greeting.new) # => "Hello, World!"
17+
render(Greeting.new, name: "Local") # => "Hello, Local!"
18+
render(renderable: Greeting.new, locals: { name: "Local" }) # => "Hello, Local!"
19+
render(Greeting.new) { "Hello, Block!" } # => "Hello, Block!"
20+
```
21+
22+
*Sean Doyle*
23+
124
* Raise `ArgumentError` if `:renderable` object does not respond to `#render_in`
225

326
*Sean Doyle*

actionview/lib/action_view/helpers/rendering_helper.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,23 @@ module RenderingHelper
2424
# If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, then:
2525
#
2626
# If an object responding to +render_in+ is passed, +render_in+ is called on the object,
27-
# passing in the current view context.
27+
# passing in the current view context, render options, and block. The
28+
# object can optionally control its rendered format by defining the +format+ method.
2829
#
2930
# Otherwise, a partial is rendered using the second parameter as the locals hash.
3031
def render(options = {}, locals = {}, &block)
3132
case options
3233
when Hash
3334
in_rendering_context(options) do |renderer|
34-
if block_given?
35+
if block_given? && !options.key?(:renderable)
3536
view_renderer.render_partial(self, options.merge(partial: options[:layout]), &block)
3637
else
37-
view_renderer.render(self, options)
38+
view_renderer.render(self, options, &block)
3839
end
3940
end
4041
else
4142
if options.respond_to?(:render_in)
42-
options.render_in(self, &block)
43+
view_renderer.render(self, renderable: options, locals: locals, &block)
4344
else
4445
view_renderer.render_partial(self, partial: options, locals: locals, &block)
4546
end

actionview/lib/action_view/renderer/renderer.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ def initialize(lookup_context)
2020
end
2121

2222
# Main render entry point shared by Action View and Action Controller.
23-
def render(context, options)
24-
render_to_object(context, options).body
23+
def render(context, options, &block)
24+
render_to_object(context, options, &block).body
2525
end
2626

27-
def render_to_object(context, options) # :nodoc:
27+
def render_to_object(context, options, &block) # :nodoc:
2828
if options.key?(:partial)
2929
render_partial_to_object(context, options)
3030
else
31-
render_template_to_object(context, options)
31+
render_template_to_object(context, options, &block)
3232
end
3333
end
3434

@@ -54,8 +54,8 @@ def cache_hits # :nodoc:
5454
end
5555

5656
private
57-
def render_template_to_object(context, options)
58-
TemplateRenderer.new(@lookup_context).render(context, options)
57+
def render_template_to_object(context, options, &block)
58+
TemplateRenderer.new(@lookup_context).render(context, options, &block)
5959
end
6060

6161
def render_partial_to_object(context, options, &block)

0 commit comments

Comments
 (0)