Class: ActionController::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/action_controller/renderer.rb

Overview

Action Controller Renderer

ActionController::Renderer allows you to render arbitrary templates without being inside a controller action.

You can get a renderer instance by calling renderer on a controller class:

ApplicationController.renderer
PostsController.renderer

and render a template by calling the #render method:

ApplicationController.renderer.render template: "posts/show", assigns: { post: Post.first }
PostsController.renderer.render :show, assigns: { post: Post.first }

As a shortcut, you can also call render directly on the controller class itself:

ApplicationController.render template: "posts/show", assigns: { post: Post.first }
PostsController.render :show, assigns: { post: Post.first }

Constant Summary collapse

DEFAULTS =
{
  method: "get",
  input: ""
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, env, defaults) ⇒ Renderer

Initializes a new Renderer.

Parameters

  • controller - The controller class to instantiate for rendering.

  • env - The Rack env to use for mocking a request when rendering. Entries can be typical Rack env keys and values, or they can be any of the following, which will be converted appropriately:

    • :http_host - The HTTP host for the incoming request. Converts to Rack’s HTTP_HOST.

    • :https - Boolean indicating whether the incoming request uses HTTPS. Converts to Rack’s HTTPS.

    • :method - The HTTP method for the incoming request, case-insensitive. Converts to Rack’s REQUEST_METHOD.

    • :script_name - The portion of the incoming request’s URL path that corresponds to the application. Converts to Rack’s SCRIPT_NAME.

    • :input - The input stream. Converts to Rack’s rack.input.

  • defaults - Default values for the Rack env. Entries are specified in the same format as env. env will be merged on top of these values. defaults will be retained when calling #new on a renderer instance.

If no http_host is specified, the env HTTP host will be derived from the routes’ default_url_options. In this case, the https boolean and the script_name will also be derived from default_url_options if they were not specified. Additionally, the https boolean will fall back to Rails.application.config.force_ssl if default_url_options does not specify a protocol.



106
107
108
109
110
111
112
113
114
115
# File 'lib/action_controller/renderer.rb', line 106

def initialize(controller, env, defaults)
  @controller = controller
  @defaults = defaults
  if env.blank? && @defaults == DEFAULTS
    @env = DEFAULT_ENV
  else
    @env = normalize_env(@defaults)
    @env.merge!(normalize_env(env)) unless env.blank?
  end
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



25
26
27
# File 'lib/action_controller/renderer.rb', line 25

def controller
  @controller
end

Class Method Details

.for(controller, env = nil, defaults = DEFAULTS) ⇒ Object

Creates a new renderer using the given controller class. See ::new.



61
62
63
# File 'lib/action_controller/renderer.rb', line 61

def self.for(controller, env = nil, defaults = DEFAULTS)
  new(controller, env, defaults)
end

.normalize_env(env) ⇒ Object

:nodoc:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/action_controller/renderer.rb', line 32

def self.normalize_env(env) # :nodoc:
  new_env = {}

  env.each_pair do |key, value|
    case key
    when :https
      value = value ? "on" : "off"
    when :method
      value = -value.upcase
    end

    key = RACK_KEY_TRANSLATION[key] || key.to_s

    new_env[key] = value
  end

  if new_env["HTTP_HOST"]
    new_env["HTTPS"] ||= "off"
    new_env["SCRIPT_NAME"] ||= ""
  end

  if new_env["HTTPS"]
    new_env["rack.url_scheme"] = new_env["HTTPS"] == "on" ? "https" : "http"
  end

  new_env
end

Instance Method Details

#defaultsObject



117
118
119
120
# File 'lib/action_controller/renderer.rb', line 117

def defaults
  @defaults = @defaults.dup if @defaults.frozen?
  @defaults
end

#new(env = nil) ⇒ Object

Creates a new renderer using the same controller, but with a new Rack env.

ApplicationController.renderer.new(method: "post")


69
70
71
# File 'lib/action_controller/renderer.rb', line 69

def new(env = nil)
  self.class.new controller, env, @defaults
end

#render(*args) ⇒ Object Also known as: render_to_string

Renders a template to a string, just like ActionController::Rendering#render_to_string.



123
124
125
126
127
128
129
130
131
# File 'lib/action_controller/renderer.rb', line 123

def render(*args)
  request = ActionDispatch::Request.new(env_for_request)
  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

Creates a new renderer using the same controller, but with the given defaults merged on top of the previous defaults.



75
76
77
# File 'lib/action_controller/renderer.rb', line 75

def with_defaults(defaults)
  self.class.new controller, @env, @defaults.merge(defaults)
end