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
|
|
|
|
|
2014-02-14 10:45:46 -05:00
|
|
|
RENDER_FORMATS_IN_PRIORITY = [:body, :text, :plain, :html]
|
|
|
|
|
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-01-17 19:06:10 -05:00
|
|
|
# Returns a renderer class (inherited from ActionController::Renderer)
|
|
|
|
# for the controller.
|
|
|
|
def renderer
|
|
|
|
@renderer ||= Renderer.for(self)
|
|
|
|
end
|
|
|
|
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:
|
2013-08-01 20:27:28 -04:00
|
|
|
raise ::AbstractController::DoubleRenderError if self.response_body
|
2010-03-07 21:23:16 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2011-04-19 05:54:12 -04:00
|
|
|
# Overwrite render_to_string because body can now be set to a rack body.
|
|
|
|
def render_to_string(*)
|
2013-09-04 14:43:31 -04:00
|
|
|
result = super
|
|
|
|
if result.respond_to?(:each)
|
2011-04-19 05:54:12 -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 = {})
|
2014-02-14 10:45:46 -05:00
|
|
|
super || _render_in_priorities(options) || ' '
|
2012-01-15 19:07:20 -05:00
|
|
|
end
|
|
|
|
|
2009-08-06 17:48:48 -04:00
|
|
|
private
|
2009-10-21 15:47:10 -04:00
|
|
|
|
2014-02-14 10:45:46 -05:00
|
|
|
def _render_in_priorities(options)
|
|
|
|
RENDER_FORMATS_IN_PRIORITY.each do |format|
|
|
|
|
return options[format] if options.key?(format)
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-01-31 15:37:58 -05:00
|
|
|
def _process_format(format, options = {})
|
2013-09-09 11:32:39 -04:00
|
|
|
super
|
2014-01-31 15:37:58 -05:00
|
|
|
|
2014-02-28 19:39:22 -05:00
|
|
|
if options[:plain]
|
2014-02-07 13:45:57 -05:00
|
|
|
self.content_type = Mime::TEXT
|
2014-02-14 15:50:08 -05:00
|
|
|
else
|
|
|
|
self.content_type ||= format.to_s
|
2014-02-07 13:45:57 -05:00
|
|
|
end
|
2013-09-09 11:32:39 -04:00
|
|
|
end
|
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
# Normalize arguments by catching blocks and setting them on :update.
|
|
|
|
def _normalize_args(action=nil, options={}, &blk) #:nodoc:
|
|
|
|
options = super
|
|
|
|
options[:update] = blk if block_given?
|
|
|
|
options
|
|
|
|
end
|
2010-02-24 13:17:24 -05:00
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
# Normalize both text and status options.
|
|
|
|
def _normalize_options(options) #:nodoc:
|
2014-02-14 10:45:46 -05:00
|
|
|
_normalize_text(options)
|
2014-01-31 15:37:58 -05:00
|
|
|
|
2014-02-14 10:45:46 -05:00
|
|
|
if options[:html]
|
|
|
|
options[:html] = ERB::Util.html_escape(options[:html])
|
2010-11-19 23:46:55 -05:00
|
|
|
end
|
2010-01-22 11:57:36 -05:00
|
|
|
|
2014-07-10 19:33:22 -04:00
|
|
|
if options.delete(:nothing)
|
|
|
|
options[:body] = nil
|
2012-01-15 12:37:27 -05:00
|
|
|
end
|
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
if options[:status]
|
|
|
|
options[:status] = Rack::Utils.status_code(options[:status])
|
2010-01-22 11:57:36 -05:00
|
|
|
end
|
2010-02-24 13:17:24 -05:00
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
super
|
|
|
|
end
|
2010-03-08 05:32:01 -05:00
|
|
|
|
2014-02-14 10:45:46 -05: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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
# Process controller specific options, as status, content-type and location.
|
|
|
|
def _process_options(options) #:nodoc:
|
|
|
|
status, content_type, location = options.values_at(:status, :content_type, :location)
|
2010-03-08 05:32:01 -05:00
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
self.status = status if status
|
|
|
|
self.content_type = content_type if content_type
|
|
|
|
self.headers["Location"] = url_for(location) if location
|
2010-03-08 05:32:01 -05:00
|
|
|
|
2010-11-19 23:46:55 -05:00
|
|
|
super
|
|
|
|
end
|
2009-08-06 17:48:48 -04:00
|
|
|
end
|
|
|
|
end
|