2017-07-24 16:20:53 -04:00
# frozen_string_literal: true
2017-10-21 09:18:17 -04:00
require " abstract_controller/error "
2016-08-06 12:51:43 -04:00
require " action_view "
require " action_view/view_paths "
require " set "
2013-07-11 08:18:06 -04:00
2009-02-24 20:25:21 -05:00
module AbstractController
2009-12-12 19:48:34 -05:00
class DoubleRenderError < Error
DEFAULT_MESSAGE = " Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like \" redirect_to(...) and return \" . "
def initialize ( message = nil )
super ( message || DEFAULT_MESSAGE )
end
end
2013-07-05 08:34:39 -04:00
module Rendering
2013-07-12 07:01:10 -04:00
extend ActiveSupport :: Concern
2013-12-04 18:17:39 -05:00
include ActionView :: ViewPaths
2013-07-11 08:18:06 -04:00
2015-01-03 10:36:33 -05:00
# Normalizes arguments, options and then delegates render_to_body and
2014-12-19 15:49:50 -05:00
# sticks the result in <tt>self.response_body</tt>.
2013-09-03 08:57:33 -04:00
def render ( * args , & block )
2013-09-09 11:05:09 -04:00
options = _normalize_render ( * args , & block )
2015-10-05 19:50:37 -04:00
rendered_body = render_to_body ( options )
2015-08-27 01:05:47 -04:00
if options [ :html ]
2015-09-08 17:57:33 -04:00
_set_html_content_type
2015-08-26 18:31:23 -04:00
else
2015-09-08 17:57:33 -04:00
_set_rendered_content_type rendered_format
2015-08-26 18:31:23 -04:00
end
2019-05-08 10:28:47 -04:00
_set_vary_header
2015-10-05 19:50:37 -04:00
self . response_body = rendered_body
2013-09-03 08:57:33 -04:00
end
2013-07-05 08:34:39 -04:00
# Raw rendering of a template to a string.
#
# It is similar to render, except that it does not
2014-12-19 15:49:50 -05:00
# set the +response_body+ and it should be guaranteed
2013-07-05 08:34:39 -04:00
# to always return a string.
#
2014-12-19 15:49:50 -05:00
# If a component extends the semantics of +response_body+
# (as ActionController extends it to be anything that
2013-07-05 08:34:39 -04:00
# responds to the method each), this method needs to be
# overridden in order to still return a string.
def render_to_string ( * args , & block )
2013-09-03 08:57:33 -04:00
options = _normalize_render ( * args , & block )
render_to_body ( options )
2013-07-05 08:34:39 -04:00
end
2013-09-09 11:05:09 -04:00
# Performs the actual template rendering.
2013-09-03 08:57:33 -04:00
def render_to_body ( options = { } )
2013-07-05 08:34:39 -04:00
end
2017-09-30 05:33:53 -04:00
# Returns Content-Type of rendered content.
2013-08-01 20:27:28 -04:00
def rendered_format
2015-10-05 01:14:04 -04:00
Mime [ :text ]
2013-08-01 20:27:28 -04:00
end
2020-03-19 15:59:46 -04:00
DEFAULT_PROTECTED_INSTANCE_VARIABLES = % i ( @_action_name @_response_body @_formats @_prefixes )
2013-09-02 17:06:14 -04:00
2013-07-05 08:34:39 -04:00
# This method should return a hash with assigns.
# You can overwrite this configuration per controller.
def view_assigns
2020-03-19 15:59:46 -04:00
variables = instance_variables - _protected_ivars
2013-11-06 17:11:37 -05:00
2020-03-19 15:59:46 -04:00
variables . each_with_object ( { } ) do | name , hash |
hash [ name . slice ( 1 , name . length ) ] = instance_variable_get ( name )
2020-03-19 14:04:49 -04:00
end
2013-07-05 08:34:39 -04:00
end
2017-10-01 17:56:57 -04:00
private
2014-12-19 15:49:50 -05:00
# Normalize args by converting <tt>render "foo"</tt> to
# <tt>render :action => "foo"</tt> and <tt>render "foo/bar"</tt> to
# <tt>render :file => "foo/bar"</tt>.
2017-10-19 16:34:47 -04:00
def _normalize_args ( action = nil , options = { } ) # :doc:
2016-01-26 21:00:05 -05:00
if action . respond_to? ( :permitted? )
if action . permitted?
action
else
raise ArgumentError , " render parameters are not permitted "
end
2016-01-25 15:21:11 -05:00
elsif action . is_a? ( Hash )
2013-09-10 10:01:02 -04:00
action
else
options
end
2013-07-05 08:34:39 -04:00
end
# Normalize options.
2017-10-19 16:34:47 -04:00
def _normalize_options ( options ) # :doc:
2013-07-05 08:34:39 -04:00
options
end
# Process extra options.
2017-10-19 16:34:47 -04:00
def _process_options ( options ) # :doc:
2013-07-05 08:34:39 -04:00
options
end
2013-09-03 08:57:33 -04:00
2013-09-09 11:32:39 -04:00
# Process the rendered format.
2017-09-30 05:33:53 -04:00
def _process_format ( format ) # :nodoc:
2013-09-09 11:32:39 -04:00
end
2016-04-15 21:41:19 -04:00
def _process_variant ( options )
end
2015-09-08 17:57:33 -04:00
def _set_html_content_type # :nodoc:
2015-08-26 18:31:23 -04:00
end
2019-05-08 10:28:47 -04:00
def _set_vary_header # :nodoc:
end
2015-09-08 17:57:33 -04:00
def _set_rendered_content_type ( format ) # :nodoc:
2015-08-26 18:31:23 -04:00
end
2013-09-03 08:57:33 -04:00
# Normalize args and options.
2017-09-30 05:33:53 -04:00
def _normalize_render ( * args , & block ) # :nodoc:
2013-09-03 08:57:33 -04:00
options = _normalize_args ( * args , & block )
2016-04-15 21:41:19 -04:00
_process_variant ( options )
2013-09-03 08:57:33 -04:00
_normalize_options ( options )
options
end
2013-11-06 17:11:37 -05:00
2020-04-07 12:40:11 -04:00
def _protected_ivars
2013-11-06 17:21:40 -05:00
DEFAULT_PROTECTED_INSTANCE_VARIABLES
2013-11-06 17:11:37 -05:00
end
2013-07-05 08:34:39 -04:00
end
2010-01-31 21:32:28 -05:00
end