2009-12-12 20:50:12 -05:00
require " abstract_controller/base "
2009-08-06 21:51:24 -04:00
require " abstract_controller/logger "
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-29 18:03:23 -04:00
include AbstractController :: Logger
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-11-10 17:13:54 -05:00
# Initialize controller with nil formats.
def initialize ( * ) #:nodoc:
@_formats = nil
super
end
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.
2009-05-12 19:21:34 -04:00
def render ( * args )
2009-05-11 13:22:07 -04:00
if response_body
2009-04-29 15:19:17 -04:00
raise AbstractController :: DoubleRenderError , " OMG "
end
2009-05-28 10:49:02 -04:00
2009-05-12 19:21:34 -04:00
self . response_body = render_to_body ( * args )
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-08-06 18:45:40 -04:00
view_context . 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
def render_to_string ( options = { } )
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)
# _partial<TrueClass, FalseClass>:: Whether or not the template to be rendered is a partial
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-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
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
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 )
handler = ActionView :: Template . handler_class_for_extension ( options [ :type ] || " erb " )
template = ActionView :: Template . new ( options [ :inline ] , " inline #{ options [ :inline ] . inspect } " , handler , { } )
options [ :_template ] = template
elsif options . key? ( :template )
options [ :_template_name ] = options [ :template ]
elsif options . key? ( :file )
options [ :_template_name ] = options [ :file ]
end
2009-05-22 15:28:40 -04:00
name = ( options [ :_template_name ] || action_name ) . to_s
2009-08-14 15:00:52 -04:00
options [ :_template ] || = with_template_cache ( name ) do
2009-09-02 18:53:38 -04:00
find_template ( name , { :formats = > formats } , options )
2009-08-14 15:00:52 -04:00
end
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-03-19 18:45:48 -04:00
self . _view_paths = paths . is_a? ( ActionView :: PathSet ) ?
paths : ActionView :: Base . process_view_paths ( paths )
end
2009-02-24 20:25:21 -05:00
end
end
2009-04-17 19:34:49 -04:00
end