Class: ActionController::Renderer
- Inherits:
-
Object
- Object
- ActionController::Renderer
- Defined in:
- lib/action_controller/renderer.rb
Overview
ActionController::Renderer allows to render arbitrary templates without requirement of being in controller actions.
You get a concrete renderer class by invoking ActionController::Base#renderer. For example,
ApplicationController.renderer
It allows you to call method #render directly.
ApplicationController.renderer.render template: '...'
You can use this shortcut in a controller, instead of the previous example:
ApplicationController.render template: '...'
#render allows you to use the same options that you can use when rendering in a controller. For example,
FooController.render :action, locals: { ... }, assigns: { ... }
The template will be rendered in a Rack environment which is accessible through ActionController::Renderer#env. You can set it up in two ways:
-
by changing renderer defaults, like
ApplicationController.renderer.defaults # => hash with default Rack environment
-
by initializing an instance of renderer by passing it a custom environment.
ApplicationController.renderer.new(method: 'post', https: true)
Constant Summary collapse
- DEFAULTS =
{ http_host: 'example.org', https: false, method: 'get', script_name: '', input: '' }.freeze
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
Class Method Summary collapse
-
.for(controller, env = {}, defaults = DEFAULTS.dup) ⇒ Object
Create a new renderer instance for a specific controller class.
Instance Method Summary collapse
-
#initialize(controller, env, defaults) ⇒ Renderer
constructor
Accepts a custom Rack environment to render templates in.
-
#new(env = {}) ⇒ Object
Create a new renderer for the same controller but with a new env.
-
#render(*args) ⇒ Object
Render templates with any options from ActionController::Base#render_to_string.
-
#with_defaults(defaults) ⇒ Object
Create a new renderer for the same controller but with new defaults.
Constructor Details
#initialize(controller, env, defaults) ⇒ Renderer
Accepts a custom Rack environment to render templates in. It will be merged with ActionController::Renderer.defaults
64 65 66 67 68 |
# File 'lib/action_controller/renderer.rb', line 64 def initialize(controller, env, defaults) @controller = controller @defaults = defaults @env = normalize_keys defaults.merge(env) end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
37 38 39 |
# File 'lib/action_controller/renderer.rb', line 37 def controller @controller end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
37 38 39 |
# File 'lib/action_controller/renderer.rb', line 37 def defaults @defaults end |
Class Method Details
.for(controller, env = {}, defaults = DEFAULTS.dup) ⇒ Object
Create a new renderer instance for a specific controller class.
48 49 50 |
# File 'lib/action_controller/renderer.rb', line 48 def self.for(controller, env = {}, defaults = DEFAULTS.dup) new(controller, env, defaults) end |
Instance Method Details
#new(env = {}) ⇒ Object
Create a new renderer for the same controller but with a new env.
53 54 55 |
# File 'lib/action_controller/renderer.rb', line 53 def new(env = {}) self.class.new controller, env, defaults end |
#render(*args) ⇒ Object
Render templates with any options from ActionController::Base#render_to_string.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/action_controller/renderer.rb', line 71 def render(*args) raise 'missing controller' unless controller request = ActionDispatch::Request.new @env request.routes = controller._routes instance = controller.new instance.set_request! request instance.set_response! controller.make_response!(request) instance.render_to_string(*args) end |
#with_defaults(defaults) ⇒ Object
Create a new renderer for the same controller but with new defaults.
58 59 60 |
# File 'lib/action_controller/renderer.rb', line 58 def with_defaults(defaults) self.class.new controller, env, self.defaults.merge(defaults) end |