2013-07-11 08:18:06 -04:00
require 'active_support/concern'
require 'active_support/core_ext/class/attribute'
2014-02-07 05:06:55 -05:00
require 'action_view'
2013-12-08 07:03:41 -05:00
require 'action_view/view_paths'
2013-11-06 17:21:40 -05:00
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
# :api: public
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
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.
# :api: plugin
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-07-19 06:02:09 -04:00
# :api: public
2013-09-03 08:57:33 -04:00
def render_to_body ( options = { } )
2013-07-05 08:34:39 -04:00
end
2013-12-03 09:04:25 -05:00
# Returns Content-Type of rendered content
2013-08-01 20:27:28 -04:00
# :api: public
def rendered_format
2015-10-05 01:14:04 -04:00
Mime [ :text ]
2013-08-01 20:27:28 -04:00
end
2015-08-15 11:45:13 -04:00
DEFAULT_PROTECTED_INSTANCE_VARIABLES = Set . new % i (
2013-09-02 17:06:14 -04:00
@_action_name @_response_body @_formats @_prefixes @_config
@_view_context_class @_view_renderer @_lookup_context
2013-11-06 16:37:24 -05:00
@_routes @_db_runtime
2015-08-15 11:45:13 -04:00
)
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.
# :api: public
def view_assigns
2013-11-06 17:11:37 -05:00
protected_vars = _protected_ivars
variables = instance_variables
variables . reject! { | s | protected_vars . include? s }
2013-11-06 16:54:15 -05:00
variables . each_with_object ( { } ) { | name , hash |
2013-11-06 16:53:52 -05:00
hash [ name . slice ( 1 , name . length ) ] = instance_variable_get ( name )
}
2013-07-05 08:34:39 -04:00
end
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>.
2013-07-05 08:34:39 -04:00
# :api: plugin
def _normalize_args ( action = nil , options = { } )
2013-09-10 10:01:02 -04:00
if action . is_a? Hash
action
else
options
end
2013-07-05 08:34:39 -04:00
end
# Normalize options.
# :api: plugin
def _normalize_options ( options )
options
end
# Process extra options.
# :api: plugin
def _process_options ( options )
options
end
2013-09-03 08:57:33 -04:00
2013-09-09 11:32:39 -04:00
# Process the rendered format.
# :api: private
2015-08-26 17:04:04 -04:00
def _process_format ( format )
2013-09-09 11:32:39 -04:00
end
2015-09-08 17:57:33 -04:00
def _set_html_content_type # :nodoc:
2015-08-26 18:31:23 -04:00
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.
# :api: private
def _normalize_render ( * args , & block )
options = _normalize_args ( * args , & block )
2013-12-03 05:17:01 -05:00
#TODO: remove defined? when we restore AP <=> AV dependency
2015-08-26 19:34:07 -04:00
if defined? ( request ) && request . variant . present?
2014-02-20 13:07:01 -05:00
options [ :variant ] = request . variant
end
2013-09-03 08:57:33 -04:00
_normalize_options ( options )
options
end
2013-11-06 17:11:37 -05:00
def _protected_ivars # :nodoc:
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