1
1
# frozen_string_literal: true
2
2
3
3
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 .
6
6
#
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:
9
8
#
10
9
# ApplicationController.renderer
10
+ # PostsController.renderer
11
11
#
12
- # It allows you to call method #render directly.
12
+ # and render a template by calling the #render method:
13
13
#
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 }
15
16
#
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 :
17
18
#
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 }
35
21
#
36
22
class Renderer
37
23
attr_reader :defaults , :controller
@@ -44,24 +30,45 @@ class Renderer
44
30
input : ""
45
31
} . freeze
46
32
47
- # Create a new renderer instance for a specific controller class.
33
+ # Creates a new renderer using the given controller class. See ::new .
48
34
def self . for ( controller , env = { } , defaults = DEFAULTS . dup )
49
35
new ( controller , env , defaults )
50
36
end
51
37
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
+ #
53
42
def new ( env = { } )
54
43
self . class . new controller , env , defaults
55
44
end
56
45
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.
58
48
def with_defaults ( defaults )
59
49
self . class . new controller , @env , self . defaults . merge ( defaults )
60
50
end
61
51
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.
65
72
def initialize ( controller , env , defaults )
66
73
@controller = controller
67
74
@defaults = defaults
0 commit comments