Skip to content

Commit a2174bd

Browse files
Revise ActionController::Renderer API docs [ci-skip]
This fixes a few inaccuracies that have been present since 2db7304. For example, `Controller.renderer` no longer returns a class, `Renderer#env` is not defined, and changing the value of `Renderer#defaults` has no direct effect. This also documents the user-friendly Rack env key variants.
1 parent 2e553ea commit a2174bd

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

actionpack/lib/action_controller/renderer.rb

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
# frozen_string_literal: true
22

33
module ActionController
4-
# ActionController::Renderer allows you to render arbitrary templates
5-
# without requirement of being in controller actions.
4+
# ActionController::Renderer allows you to render arbitrary templates without
5+
# being inside a controller action.
66
#
7-
# You get a concrete renderer class by invoking ActionController::Base#renderer.
8-
# For example:
7+
# You can get a renderer instance by calling +renderer+ on a controller class:
98
#
109
# ApplicationController.renderer
10+
# PostsController.renderer
1111
#
12-
# It allows you to call method #render directly.
12+
# and render a template by calling the #render method:
1313
#
14-
# ApplicationController.renderer.render template: '...'
14+
# ApplicationController.renderer.render template: "posts/show", assigns: { post: Post.first }
15+
# PostsController.renderer.render :show, assigns: { post: Post.first }
1516
#
16-
# You can use this shortcut in a controller, instead of the previous example:
17+
# As a shortcut, you can also call +render+ directly on the controller class itself:
1718
#
18-
# ApplicationController.render template: '...'
19-
#
20-
# #render allows you to use the same options that you can use when rendering in a controller.
21-
# For example:
22-
#
23-
# FooController.render :action, locals: { ... }, assigns: { ... }
24-
#
25-
# The template will be rendered in a Rack environment which is accessible through
26-
# ActionController::Renderer#env. You can set it up in two ways:
27-
#
28-
# * by changing renderer defaults, like
29-
#
30-
# ApplicationController.renderer.defaults # => hash with default Rack environment
31-
#
32-
# * by initializing an instance of renderer by passing it a custom environment.
33-
#
34-
# ApplicationController.renderer.new(method: 'post', https: true)
19+
# ApplicationController.render template: "posts/show", assigns: { post: Post.first }
20+
# PostsController.render :show, assigns: { post: Post.first }
3521
#
3622
class Renderer
3723
attr_reader :defaults, :controller
@@ -44,24 +30,45 @@ class Renderer
4430
input: ""
4531
}.freeze
4632

47-
# Create a new renderer instance for a specific controller class.
33+
# Creates a new renderer using the given controller class. See ::new.
4834
def self.for(controller, env = {}, defaults = DEFAULTS.dup)
4935
new(controller, env, defaults)
5036
end
5137

52-
# Create a new renderer for the same controller but with a new env.
38+
# Creates a new renderer using the same controller, but with a new Rack env.
39+
#
40+
# ApplicationController.renderer.new(method: "post")
41+
#
5342
def new(env = {})
5443
self.class.new controller, env, defaults
5544
end
5645

57-
# Create a new renderer for the same controller but with new defaults.
46+
# Creates a new renderer using the same controller, but with the given
47+
# defaults merged on top of the previous defaults.
5848
def with_defaults(defaults)
5949
self.class.new controller, @env, self.defaults.merge(defaults)
6050
end
6151

62-
# Accepts a custom Rack environment to render templates in.
63-
# It will be merged with the default Rack environment defined by
64-
# +ActionController::Renderer::DEFAULTS+.
52+
# Initializes a new Renderer.
53+
#
54+
# ==== Parameters
55+
#
56+
# * +controller+ - The controller class to instantiate for rendering.
57+
# * +env+ - The Rack env to use for mocking a request when rendering.
58+
# Entries can be typical Rack env keys and values, or they can be any of
59+
# the following, which will be converted appropriately:
60+
# * +:http_host+ - The HTTP host for the incoming request. Converts to
61+
# Rack's +HTTP_HOST+.
62+
# * +:https+ - Boolean indicating whether the incoming request uses HTTPS.
63+
# Converts to Rack's +HTTPS+.
64+
# * +:method+ - The HTTP method for the incoming request, case-insensitive.
65+
# Converts to Rack's +REQUEST_METHOD+.
66+
# * +:script_name+ - The portion of the incoming request's URL path that
67+
# corresponds to the application. Converts to Rack's +SCRIPT_NAME+.
68+
# * +:input+ - The input stream. Converts to Rack's +rack.input+.
69+
# * +defaults+ - Default values for the Rack env. Entries are specified in
70+
# the same format as +env+. +env+ will be merged on top of these values.
71+
# +defaults+ will be retained when calling #new on a renderer instance.
6572
def initialize(controller, env, defaults)
6673
@controller = controller
6774
@defaults = defaults

0 commit comments

Comments
 (0)