2016-08-06 12:51:43 -04:00
|
|
|
require "active_support/core_ext/hash/keys"
|
2015-01-22 11:35:19 -05:00
|
|
|
|
2015-01-17 19:06:10 -05:00
|
|
|
module ActionController
|
2016-06-10 08:57:15 -04:00
|
|
|
# ActionController::Renderer allows you to render arbitrary templates
|
2015-01-17 19:06:10 -05:00
|
|
|
# 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: '...'
|
|
|
|
#
|
2016-05-04 02:00:47 -04:00
|
|
|
# You can use this shortcut in a controller, instead of the previous example:
|
2015-01-17 19:06:10 -05:00
|
|
|
#
|
|
|
|
# ApplicationController.render template: '...'
|
|
|
|
#
|
2016-05-04 02:00:47 -04:00
|
|
|
# #render allows you to use the same options that you can use when rendering in a controller.
|
2015-01-17 19:06:10 -05:00
|
|
|
# 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)
|
|
|
|
#
|
|
|
|
class Renderer
|
2015-09-14 18:58:12 -04:00
|
|
|
attr_reader :defaults, :controller
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
DEFAULTS = {
|
2016-08-06 12:51:43 -04:00
|
|
|
http_host: "example.org",
|
2015-09-14 18:58:12 -04:00
|
|
|
https: false,
|
2016-08-06 12:51:43 -04:00
|
|
|
method: "get",
|
|
|
|
script_name: "",
|
|
|
|
input: ""
|
2015-09-14 18:58:12 -04:00
|
|
|
}.freeze
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
# Create a new renderer instance for a specific controller class.
|
2016-04-20 19:47:16 -04:00
|
|
|
def self.for(controller, env = {}, defaults = DEFAULTS.dup)
|
2015-09-14 18:58:12 -04:00
|
|
|
new(controller, env, defaults)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new renderer for the same controller but with a new env.
|
|
|
|
def new(env = {})
|
|
|
|
self.class.new controller, env, defaults
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new renderer for the same controller but with new defaults.
|
|
|
|
def with_defaults(defaults)
|
|
|
|
self.class.new controller, env, self.defaults.merge(defaults)
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Accepts a custom Rack environment to render templates in.
|
2017-01-12 06:52:07 -05:00
|
|
|
# It will be merged with the default Rack environment defined by
|
|
|
|
# +ActionController::Renderer::DEFAULTS+.
|
2015-09-14 18:58:12 -04:00
|
|
|
def initialize(controller, env, defaults)
|
|
|
|
@controller = controller
|
|
|
|
@defaults = defaults
|
2015-09-14 19:25:48 -04:00
|
|
|
@env = normalize_keys defaults.merge(env)
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Render templates with any options from ActionController::Base#render_to_string.
|
|
|
|
def render(*args)
|
2016-08-06 12:51:43 -04:00
|
|
|
raise "missing controller" unless controller
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-15 10:38:39 -04:00
|
|
|
request = ActionDispatch::Request.new @env
|
|
|
|
request.routes = controller._routes
|
|
|
|
|
|
|
|
instance = controller.new
|
|
|
|
instance.set_request! request
|
|
|
|
instance.set_response! controller.make_response!(request)
|
2015-01-17 19:06:10 -05:00
|
|
|
instance.render_to_string(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def normalize_keys(env)
|
2015-09-14 18:58:12 -04:00
|
|
|
new_env = {}
|
2016-10-28 23:05:58 -04:00
|
|
|
env.each_pair { |k, v| new_env[rack_key_for(k)] = rack_value_for(k, v) }
|
2017-03-02 04:54:58 -05:00
|
|
|
new_env["rack.url_scheme"] = new_env["HTTPS"] == "on" ? "https" : "http"
|
2015-09-14 18:58:12 -04:00
|
|
|
new_env
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
RACK_KEY_TRANSLATION = {
|
2016-08-06 12:51:43 -04:00
|
|
|
http_host: "HTTP_HOST",
|
|
|
|
https: "HTTPS",
|
|
|
|
method: "REQUEST_METHOD",
|
|
|
|
script_name: "SCRIPT_NAME",
|
|
|
|
input: "rack.input"
|
2015-09-14 18:58:12 -04:00
|
|
|
}
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
IDENTITY = ->(_) { _ }
|
|
|
|
|
|
|
|
RACK_VALUE_TRANSLATION = {
|
2016-08-06 12:51:43 -04:00
|
|
|
https: ->(v) { v ? "on" : "off" },
|
2015-09-14 18:58:12 -04:00
|
|
|
method: ->(v) { v.upcase },
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:53:41 -04:00
|
|
|
def rack_key_for(key)
|
|
|
|
RACK_KEY_TRANSLATION.fetch(key, key.to_s)
|
|
|
|
end
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
def rack_value_for(key, value)
|
|
|
|
RACK_VALUE_TRANSLATION.fetch(key, IDENTITY).call value
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|