2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-08-06 17:48:48 -04:00
|
|
|
module ActionController
|
2009-12-20 20:15:31 -05:00
|
|
|
module Rendering
|
2009-08-06 17:48:48 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2016-05-21 08:49:38 -04:00
|
|
|
RENDER_FORMATS_IN_PRIORITY = [:body, :plain, :html]
|
2014-02-14 10:45:46 -05:00
|
|
|
|
2015-01-17 19:06:10 -05:00
|
|
|
module ClassMethods
|
2015-01-21 16:23:22 -05:00
|
|
|
# Documentation at ActionController::Renderer#render
|
|
|
|
delegate :render, to: :renderer
|
|
|
|
|
2015-09-14 18:23:25 -04:00
|
|
|
# Returns a renderer instance (inherited from ActionController::Renderer)
|
2015-01-17 19:06:10 -05:00
|
|
|
# for the controller.
|
2015-09-14 18:29:19 -04:00
|
|
|
attr_reader :renderer
|
|
|
|
|
|
|
|
def setup_renderer! # :nodoc:
|
|
|
|
@renderer = Renderer.for(self)
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
2015-09-14 18:23:25 -04:00
|
|
|
|
|
|
|
def inherited(klass)
|
2015-09-14 18:29:19 -04:00
|
|
|
klass.setup_renderer!
|
2015-09-14 18:23:25 -04:00
|
|
|
super
|
|
|
|
end
|
2015-01-17 19:06:10 -05:00
|
|
|
end
|
|
|
|
|
2010-03-12 14:39:53 -05:00
|
|
|
# Before processing, set the request formats in current controller formats.
|
2010-03-13 15:28:34 -05:00
|
|
|
def process_action(*) #:nodoc:
|
2013-04-10 03:18:06 -04:00
|
|
|
self.formats = request.formats.map(&:ref).compact
|
2009-08-06 17:48:48 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-03-12 14:39:53 -05:00
|
|
|
# Check for double render errors and set the content_type after rendering.
|
|
|
|
def render(*args) #:nodoc:
|
2016-08-07 19:05:28 -04:00
|
|
|
raise ::AbstractController::DoubleRenderError if response_body
|
2010-03-07 21:23:16 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2017-03-11 08:17:44 -05:00
|
|
|
# Overwrite render_to_string because body can now be set to a Rack body.
|
2011-04-19 05:54:12 -04:00
|
|
|
def render_to_string(*)
|
2013-09-04 14:43:31 -04:00
|
|
|
result = super
|
|
|
|
if result.respond_to?(:each)
|
2018-05-17 04:32:27 -04:00
|
|
|
string = +""
|
2013-09-04 14:43:31 -04:00
|
|
|
result.each { |r| string << r }
|
2011-04-19 05:54:12 -04:00
|
|
|
string
|
2013-09-04 14:43:31 -04:00
|
|
|
else
|
|
|
|
result
|
2011-04-19 05:54:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-09 11:05:09 -04:00
|
|
|
def render_to_body(options = {})
|
2016-08-06 12:51:43 -04:00
|
|
|
super || _render_in_priorities(options) || " "
|
2012-01-15 19:07:20 -05:00
|
|
|
end
|
|
|
|
|
2009-08-06 17:48:48 -04:00
|
|
|
private
|
2016-04-15 21:41:19 -04:00
|
|
|
def _process_variant(options)
|
|
|
|
if defined?(request) && !request.nil? && request.variant.present?
|
|
|
|
options[:variant] = request.variant
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def _render_in_priorities(options)
|
|
|
|
RENDER_FORMATS_IN_PRIORITY.each do |format|
|
|
|
|
return options[format] if options.key?(format)
|
|
|
|
end
|
2014-02-14 10:45:46 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
nil
|
|
|
|
end
|
2014-02-14 10:45:46 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def _set_html_content_type
|
|
|
|
self.content_type = Mime[:html].to_s
|
|
|
|
end
|
2014-01-31 15:37:58 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def _set_rendered_content_type(format)
|
2019-04-17 02:37:16 -04:00
|
|
|
if format && !response.media_type
|
2016-08-06 13:55:02 -04:00
|
|
|
self.content_type = format.to_s
|
|
|
|
end
|
2015-09-08 17:57:33 -04:00
|
|
|
end
|
2013-09-09 11:32:39 -04:00
|
|
|
|
2019-05-08 10:28:47 -04:00
|
|
|
def _set_vary_header
|
|
|
|
self.headers["Vary"] = "Accept" if request.should_apply_vary_header?
|
|
|
|
end
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# Normalize arguments by catching blocks and setting them on :update.
|
2016-12-23 23:26:29 -05:00
|
|
|
def _normalize_args(action = nil, options = {}, &blk)
|
2016-08-06 13:55:02 -04:00
|
|
|
options = super
|
|
|
|
options[:update] = blk if block_given?
|
|
|
|
options
|
|
|
|
end
|
2010-02-24 13:17:24 -05:00
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# Normalize both text and status options.
|
2016-12-23 23:26:29 -05:00
|
|
|
def _normalize_options(options)
|
2016-08-06 13:55:02 -04:00
|
|
|
_normalize_text(options)
|
2014-01-31 15:37:58 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
if options[:html]
|
|
|
|
options[:html] = ERB::Util.html_escape(options[:html])
|
|
|
|
end
|
2010-01-22 11:57:36 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
if options[:status]
|
|
|
|
options[:status] = Rack::Utils.status_code(options[:status])
|
|
|
|
end
|
2010-02-24 13:17:24 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
super
|
|
|
|
end
|
2010-03-08 05:32:01 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def _normalize_text(options)
|
|
|
|
RENDER_FORMATS_IN_PRIORITY.each do |format|
|
|
|
|
if options.key?(format) && options[format].respond_to?(:to_text)
|
|
|
|
options[format] = options[format].to_text
|
|
|
|
end
|
2014-02-14 10:45:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-14 04:57:52 -04:00
|
|
|
# Process controller specific options, as status, content-type and location.
|
2016-12-23 23:26:29 -05:00
|
|
|
def _process_options(options)
|
2016-08-06 13:55:02 -04:00
|
|
|
status, content_type, location = options.values_at(:status, :content_type, :location)
|
2010-03-08 05:32:01 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
self.status = status if status
|
|
|
|
self.content_type = content_type if content_type
|
2017-01-05 03:20:57 -05:00
|
|
|
headers["Location"] = url_for(location) if location
|
2010-03-08 05:32:01 -05:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
super
|
|
|
|
end
|
2009-08-06 17:48:48 -04:00
|
|
|
end
|
|
|
|
end
|