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
2010-03-08 05:32:01 -05:00
module Rendering
extend ActiveSupport :: Concern
include AbstractController :: ViewPaths
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:
2010-03-08 05:32:01 -05:00
# View.for_controller[controller]
# Create a new ActionView instance for a controller
# View#render_template[options]
# Returns String with the rendered template
2009-06-08 20:41:08 -04:00
#
2010-02-16 13:45:59 -05:00
# Override this method in a module 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 )
2010-03-07 21:23:16 -05:00
options = _normalize_args ( * args , & block )
_normalize_options ( options )
2010-01-20 08:21:27 -05:00
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-03-19 18:45:48 -04:00
# :api: plugin
2009-04-17 19:34:49 -04:00
def render_to_body ( options = { } )
2010-03-08 05:32:01 -05:00
_process_options ( options )
_render_template ( options )
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-04-21 19:02:30 -04:00
# :api: plugin
2010-03-07 21:23:16 -05:00
def render_to_string ( options = { } )
_normalize_options ( 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
2010-03-08 05:32:01 -05:00
# Find and renders a template based on the options given.
2009-05-22 15:28:40 -04:00
def _render_template ( options )
2010-03-08 05:32:01 -05:00
view_context . render_template ( options ) { | template | _with_template_hook ( template ) }
2009-05-28 10:49:02 -04:00
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-03-08 10:32:40 -05:00
# Normalize options by converting render "foo" to render :action => "foo" and
# render "foo/bar" to render :file => "foo/bar".
2010-03-07 21:23:16 -05:00
def _normalize_args ( action = nil , options = { } )
2010-01-22 11:57:36 -05:00
case action
2010-03-08 05:32:01 -05:00
when NilClass
2010-01-22 11:57:36 -05:00
when Hash
options , action = action , nil
when String , Symbol
action = action . to_s
2010-03-08 10:32:40 -05:00
key = action . include? ( ?/ ) ? :file : :action
options [ key ] = action
2010-03-08 05:32:01 -05:00
else
options . merge! ( :partial = > action )
2010-01-22 11:57:36 -05:00
end
options
end
2010-03-07 21:23:16 -05:00
def _normalize_options ( options )
2010-03-08 05:32:01 -05:00
if options [ :partial ] == true
options [ :partial ] = action_name
end
2010-03-07 21:23:16 -05:00
2010-03-08 05:32:01 -05:00
if ( options . keys & [ :partial , :file , :template ] ) . empty?
2010-03-08 10:50:10 -05:00
options [ :_prefix ] || = _prefix
2009-10-18 20:52:36 -04:00
end
2010-03-08 05:32:01 -05:00
options [ :template ] || = ( options [ :action ] || action_name ) . to_s
options
2010-02-24 16:06:24 -05:00
end
2010-03-08 05:32:01 -05:00
def _process_options ( options )
2009-10-21 13:55:47 -04:00
end
2010-03-08 05:32:01 -05:00
def _with_template_hook ( template )
self . formats = template . details [ :formats ]
2009-02-24 20:25:21 -05:00
end
end
2010-01-31 21:32:28 -05:00
end