2015-01-22 11:35:19 -05:00
|
|
|
require 'active_support/core_ext/hash/keys'
|
|
|
|
|
2015-01-17 19:06:10 -05:00
|
|
|
module ActionController
|
|
|
|
# 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 a shortcut on controller to replace previous example with:
|
|
|
|
#
|
|
|
|
# ApplicationController.render template: '...'
|
|
|
|
#
|
|
|
|
# #render method allows you to use any options as when rendering in 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)
|
|
|
|
#
|
|
|
|
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 = {
|
|
|
|
http_host: 'example.org',
|
|
|
|
https: false,
|
|
|
|
method: 'get',
|
|
|
|
script_name: '',
|
|
|
|
input: ''
|
|
|
|
}.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.
|
|
|
|
def self.for(controller, env = {}, defaults = DEFAULTS)
|
|
|
|
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.
|
|
|
|
# It will be merged with 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)
|
2015-09-14 18:58:12 -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 = {}
|
|
|
|
env.each_pair { |k,v| new_env[rack_key_for(k)] = rack_value_for(k, v) }
|
|
|
|
new_env
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
RACK_KEY_TRANSLATION = {
|
|
|
|
http_host: 'HTTP_HOST',
|
|
|
|
https: 'HTTPS',
|
|
|
|
method: 'REQUEST_METHOD',
|
|
|
|
script_name: 'SCRIPT_NAME',
|
|
|
|
input: 'rack.input'
|
|
|
|
}
|
2015-01-17 19:06:10 -05:00
|
|
|
|
2015-09-14 18:58:12 -04:00
|
|
|
IDENTITY = ->(_) { _ }
|
|
|
|
|
|
|
|
RACK_VALUE_TRANSLATION = {
|
|
|
|
https: ->(v) { v ? 'on' : 'off' },
|
|
|
|
method: ->(v) { v.upcase },
|
|
|
|
}
|
|
|
|
|
|
|
|
def rack_key_for(key); RACK_KEY_TRANSLATION[key]; 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
|