mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Cleanup ActionController::Rendering
This commit is contained in:
parent
243e6e4b2a
commit
79c4983f89
1 changed files with 28 additions and 12 deletions
|
@ -2,6 +2,8 @@ module ActionController
|
||||||
module Rendering
|
module Rendering
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
RENDER_FORMATS_IN_PRIORITY = [:body, :text, :plain, :html]
|
||||||
|
|
||||||
# Before processing, set the request formats in current controller formats.
|
# Before processing, set the request formats in current controller formats.
|
||||||
def process_action(*) #:nodoc:
|
def process_action(*) #:nodoc:
|
||||||
self.formats = request.formats.map(&:ref).compact
|
self.formats = request.formats.map(&:ref).compact
|
||||||
|
@ -27,11 +29,19 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_to_body(options = {})
|
def render_to_body(options = {})
|
||||||
super || options[:body].presence || options[:text].presence || options[:plain].presence || ERB::Util.h(options[:html]).presence || ' '
|
super || _render_in_priorities(options) || ' '
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def _render_in_priorities(options)
|
||||||
|
RENDER_FORMATS_IN_PRIORITY.each do |format|
|
||||||
|
return options[format] if options.key?(format)
|
||||||
|
end
|
||||||
|
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
def _process_format(format, options = {})
|
def _process_format(format, options = {})
|
||||||
super
|
super
|
||||||
self.content_type ||= format.to_s
|
self.content_type ||= format.to_s
|
||||||
|
@ -55,19 +65,13 @@ module ActionController
|
||||||
|
|
||||||
# Normalize both text and status options.
|
# Normalize both text and status options.
|
||||||
def _normalize_options(options) #:nodoc:
|
def _normalize_options(options) #:nodoc:
|
||||||
if options.key?(:body) && options[:body].respond_to?(:to_text)
|
_normalize_text(options)
|
||||||
options[:body] = options[:body].to_text
|
|
||||||
|
if options[:html]
|
||||||
|
options[:html] = ERB::Util.html_escape(options[:html])
|
||||||
end
|
end
|
||||||
|
|
||||||
if options.key?(:text) && options[:text].respond_to?(:to_text)
|
if options.delete(:nothing) || _any_render_format_is_nil?(options)
|
||||||
options[:text] = options[:text].to_text
|
|
||||||
end
|
|
||||||
|
|
||||||
if options.key?(:plain) && options[:plain].respond_to?(:to_text)
|
|
||||||
options[:plain] = options[:plain].to_text
|
|
||||||
end
|
|
||||||
|
|
||||||
if options.delete(:nothing) || (options.key?(:body) && options[:body].nil?) || (options.key?(:text) && options[:text].nil?) || (options.key?(:plain) && options[:plain].nil?) || (options.key?(:html) && options[:html].nil?)
|
|
||||||
options[:body] = " "
|
options[:body] = " "
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,6 +82,18 @@ module ActionController
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def _any_render_format_is_nil?(options)
|
||||||
|
RENDER_FORMATS_IN_PRIORITY.any? { |format| options.key?(format) && options[format].nil? }
|
||||||
|
end
|
||||||
|
|
||||||
# Process controller specific options, as status, content-type and location.
|
# Process controller specific options, as status, content-type and location.
|
||||||
def _process_options(options) #:nodoc:
|
def _process_options(options) #:nodoc:
|
||||||
status, content_type, location = options.values_at(:status, :content_type, :location)
|
status, content_type, location = options.values_at(:status, :content_type, :location)
|
||||||
|
|
Loading…
Reference in a new issue