2009-12-12 20:50:12 -05:00
require " abstract_controller/base "
2009-02-27 14:42:13 -05: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
2009-12-20 20:15:31 -05:00
module Rendering
2009-05-28 12:35:36 -04:00
extend ActiveSupport :: Concern
2009-05-07 11:29:22 -04:00
2009-05-07 11:38:57 -04:00
included do
2009-04-07 17:57:18 -04:00
extlib_inheritable_accessor :_view_paths
self . _view_paths || = ActionView :: PathSet . new
2009-02-24 20:25:21 -05:00
end
2009-05-07 11:38:57 -04:00
2009-06-08 20:41:08 -04:00
# An instance of a view class. The default view class is ActionView::Base
#
# The view class must have the following methods:
2009-12-12 19:48:34 -05:00
# View.for_controller[controller] Create a new ActionView instance for a
2009-07-19 08:53:02 -04:00
# controller
2009-08-06 18:43:28 -04:00
# View#render_partial[options]
2009-07-19 08:53:02 -04:00
# - responsible for setting options[:_template]
# - Returns String with the rendered partial
2009-06-08 20:41:08 -04:00
# options<Hash>:: see _render_partial in ActionView::Base
2009-08-06 18:43:28 -04:00
# View#render_template[template, layout, options, partial]
2009-07-19 08:53:02 -04:00
# - Returns String with the rendered template
2009-06-08 20:41:08 -04:00
# template<ActionView::Template>:: The template to render
# layout<ActionView::Template>:: The layout to render around the template
# options<Hash>:: See _render_template_with_layout in ActionView::Base
# partial<Boolean>:: Whether or not the template to render is a partial
#
# Override this method in a to change the default behavior.
2009-08-06 18:45:40 -04:00
def view_context
@_view_context || = ActionView :: Base . for_controller ( self )
2009-02-24 20:25:21 -05:00
end
2009-05-28 10:49:02 -04:00
2009-06-08 20:41:08 -04:00
# Mostly abstracts the fact that calling render twice is a DoubleRenderError.
# Delegates render_to_body and sticks the result in self.response_body.
2010-01-20 08:21:27 -05:00
def render ( * args , & block )
options = _normalize_options ( * args , & block )
self . response_body = render_to_body ( options )
2009-03-18 18:58:47 -04:00
end
2009-05-28 10:49:02 -04:00
2009-04-21 19:02:30 -04:00
# Raw rendering of a template to a Rack-compatible body.
2009-06-08 20:41:08 -04:00
#
# ==== Options
# _partial_object<Object>:: The object that is being rendered. If this
# exists, we are in the special case of rendering an object as a partial.
2009-05-28 10:49:02 -04:00
#
2009-03-19 18:45:48 -04:00
# :api: plugin
2009-04-17 19:34:49 -04:00
def render_to_body ( options = { } )
2009-05-12 19:21:34 -04:00
# TODO: Refactor so we can just use the normal template logic for this
2009-08-07 10:33:54 -04:00
if options . key? ( :partial )
2009-12-30 19:45:56 -05:00
_render_partial ( options )
2009-05-22 15:28:40 -04:00
else
_determine_template ( options )
_render_template ( options )
2009-05-12 19:21:34 -04:00
end
2009-02-27 14:42:13 -05:00
end
2009-06-08 20:41:08 -04:00
# Raw rendering of a template to a string. Just convert the results of
# render_to_body into a String.
2009-05-28 10:49:02 -04:00
#
2009-04-21 19:02:30 -04:00
# :api: plugin
2010-01-20 08:21:27 -05:00
def render_to_string ( * args )
options = _normalize_options ( * args )
2009-12-20 20:15:31 -05:00
AbstractController :: Rendering . body_to_s ( render_to_body ( options ) )
2009-04-21 19:02:30 -04:00
end
2009-06-08 20:41:08 -04:00
# Renders the template from an object.
#
# ==== Options
# _template<ActionView::Template>:: The template to render
# _layout<ActionView::Template>:: The layout to wrap the template in (optional)
2009-05-22 15:28:40 -04:00
def _render_template ( options )
2009-08-07 00:51:50 -04:00
view_context . render_template ( options )
2009-02-27 14:42:13 -05:00
end
2009-05-28 10:49:02 -04:00
2009-12-30 19:45:56 -05:00
# Renders the given partial.
#
# ==== Options
# partial<String|Object>:: The partial name or the object to be rendered
def _render_partial ( options )
view_context . render_partial ( options )
end
2009-06-08 20:41:08 -04:00
# The list of view paths for this controller. See ActionView::ViewPathSet for
# more details about writing custom view paths.
def view_paths
2009-05-28 10:49:02 -04:00
_view_paths
end
2009-02-27 14:42:13 -05:00
2010-01-22 11:57:36 -05:00
# The prefix used in render "foo" shortcuts.
def _prefix
controller_path
2010-01-20 08:21:27 -05:00
end
2009-04-22 03:22:07 -04:00
# Return a string representation of a Rack-compatible response body.
def self . body_to_s ( body )
if body . respond_to? ( :to_str )
body
else
strings = [ ]
body . each { | part | strings << part . to_s }
body . close if body . respond_to? ( :close )
strings . join
end
end
2009-05-22 15:28:40 -04:00
private
2009-10-21 13:55:47 -04:00
2010-01-22 11:57:36 -05:00
# Normalize options, by converting render "foo" to render :template => "prefix/foo"
# and render "/foo" to render :file => "/foo".
def _normalize_options ( action = nil , options = { } )
case action
when Hash
options , action = action , nil
when String , Symbol
action = action . to_s
case action . index ( " / " )
when NilClass
options [ :_prefix ] = _prefix
options [ :_template_name ] = action
when 0
options [ :file ] = action
else
options [ :template ] = action
end
end
options
end
2009-06-08 20:41:08 -04:00
# Take in a set of options and determine the template to render
#
# ==== Options
# _template<ActionView::Template>:: If this is provided, the search is over
# _template_name<#to_s>:: The name of the template to look up. Otherwise,
# use the current action name.
# _prefix<String>:: The prefix to look inside of. In a file system, this corresponds
# to a directory.
# _partial<TrueClass, FalseClass>:: Whether or not the file to look up is a partial
2009-05-22 15:28:40 -04:00
def _determine_template ( options )
2009-10-18 20:52:36 -04:00
if options . key? ( :text )
2009-12-02 23:01:01 -05:00
options [ :_template ] = ActionView :: Template :: Text . new ( options [ :text ] , format_for_text )
2009-10-18 20:52:36 -04:00
elsif options . key? ( :inline )
2009-12-21 19:03:04 -05:00
handler = ActionView :: Template . handler_class_for_extension ( options [ :type ] || " erb " )
template = ActionView :: Template . new ( options [ :inline ] , " inline template " , handler , { } )
2009-10-18 20:52:36 -04:00
options [ :_template ] = template
elsif options . key? ( :template )
options [ :_template_name ] = options [ :template ]
elsif options . key? ( :file )
options [ :_template_name ] = options [ :file ]
end
2010-01-29 10:49:26 -05:00
name = ( options [ :_template_name ] || options [ :action ] || action_name ) . to_s
options [ :_prefix ] || = _prefix if ( options . keys & [ :partial , :file , :template ] ) . empty?
2009-05-22 15:28:40 -04:00
2010-01-29 11:21:31 -05:00
details = _normalize_details ( options )
2009-08-14 15:00:52 -04:00
options [ :_template ] || = with_template_cache ( name ) do
2010-01-29 11:21:31 -05:00
find_template ( name , details , options )
2009-08-14 15:00:52 -04:00
end
end
2009-09-02 18:53:38 -04:00
2010-01-29 11:21:31 -05:00
def _normalize_details ( options )
details = { :formats = > formats }
details [ :formats ] = Array ( options [ :format ] ) if options [ :format ]
details [ :locale ] = Array ( options [ :locale ] ) if options [ :locale ]
details
end
2009-09-02 18:53:38 -04:00
def find_template ( name , details , options )
view_paths . find ( name , details , options [ :_prefix ] , options [ :_partial ] )
end
2009-09-03 15:41:28 -04:00
def template_exists? ( name , details , options )
view_paths . exists? ( name , details , options [ :_prefix ] , options [ :_partial ] )
end
2009-08-14 15:00:52 -04:00
def with_template_cache ( name )
yield
2009-05-22 15:28:40 -04:00
end
2009-10-21 13:55:47 -04:00
def format_for_text
Mime [ :text ]
end
2009-02-24 20:25:21 -05:00
module ClassMethods
2009-08-14 15:00:52 -04:00
def clear_template_caches!
end
2009-12-12 19:48:34 -05:00
2009-06-08 20:41:08 -04:00
# Append a path to the list of view paths for this controller.
#
# ==== Parameters
2009-12-12 19:48:34 -05:00
# path<String, ViewPath>:: If a String is provided, it gets converted into
# the default view path. You may also provide a custom view path
2009-06-08 20:41:08 -04:00
# (see ActionView::ViewPathSet for more information)
2009-02-24 20:25:21 -05:00
def append_view_path ( path )
self . view_paths << path
end
2009-05-28 10:49:02 -04:00
2009-06-08 20:41:08 -04:00
# Prepend a path to the list of view paths for this controller.
#
# ==== Parameters
2009-12-12 19:48:34 -05:00
# path<String, ViewPath>:: If a String is provided, it gets converted into
# the default view path. You may also provide a custom view path
2009-06-08 20:41:08 -04:00
# (see ActionView::ViewPathSet for more information)
2009-05-26 15:14:50 -04:00
def prepend_view_path ( path )
2009-08-14 15:00:52 -04:00
clear_template_caches!
2009-05-26 15:14:50 -04:00
self . view_paths . unshift ( path )
end
2009-06-08 20:41:08 -04:00
# A list of all of the default view paths for this controller.
2009-03-19 18:45:48 -04:00
def view_paths
self . _view_paths
end
2009-05-28 10:49:02 -04:00
2009-06-08 20:41:08 -04:00
# Set the view paths.
#
# ==== Parameters
# paths<ViewPathSet, Object>:: If a ViewPathSet is provided, use that;
# otherwise, process the parameter into a ViewPathSet.
2009-03-19 18:45:48 -04:00
def view_paths = ( paths )
2009-08-14 15:00:52 -04:00
clear_template_caches!
2009-12-21 19:03:04 -05:00
self . _view_paths = paths . is_a? ( ActionView :: PathSet ) ? paths : ActionView :: Base . process_view_paths ( paths )
2009-03-19 18:45:48 -04:00
end
2009-02-24 20:25:21 -05:00
end
end
2009-12-21 19:03:04 -05:00
end