2009-02-27 14:42:13 -05:00
|
|
|
require "action_controller/abstract/logger"
|
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
module AbstractController
|
|
|
|
module Renderer
|
2009-05-07 11:29:22 -04:00
|
|
|
extend ActiveSupport::DependencyModule
|
|
|
|
|
2009-04-07 17:57:18 -04:00
|
|
|
depends_on AbstractController::Logger
|
2009-02-24 20:25:21 -05:00
|
|
|
|
2009-04-07 17:57:18 -04:00
|
|
|
setup do
|
|
|
|
attr_internal :formats
|
|
|
|
|
|
|
|
extlib_inheritable_accessor :_view_paths
|
|
|
|
|
|
|
|
self._view_paths ||= ActionView::PathSet.new
|
2009-02-24 20:25:21 -05:00
|
|
|
end
|
2009-04-07 17:57:18 -04:00
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
def _action_view
|
|
|
|
@_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self)
|
|
|
|
end
|
2009-03-19 18:45:48 -04:00
|
|
|
|
2009-03-20 19:50:51 -04:00
|
|
|
def render(options = {})
|
2009-04-17 19:34:49 -04:00
|
|
|
self.response_body = render_to_body(options)
|
2009-03-18 18:58:47 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
# ====
|
|
|
|
# @option _prefix<String> The template's path prefix
|
|
|
|
# @option _layout<String> The relative path to the layout template to use
|
|
|
|
#
|
|
|
|
# :api: plugin
|
2009-04-17 19:34:49 -04:00
|
|
|
def render_to_body(options = {})
|
2009-03-20 19:50:51 -04:00
|
|
|
name = options[:_template_name] || action_name
|
|
|
|
|
2009-04-22 20:16:28 -04:00
|
|
|
template = options[:_template] || view_paths.find_by_parts(name.to_s, {:formats => formats}, options[:_prefix])
|
2009-03-19 18:45:48 -04:00
|
|
|
_render_template(template, options)
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
|
|
|
|
2009-04-21 19:02:30 -04:00
|
|
|
# Raw rendering of a template to a string.
|
|
|
|
# ====
|
|
|
|
# @option _prefix<String> The template's path prefix
|
|
|
|
# @option _layout<String> The relative path to the layout template to use
|
|
|
|
#
|
|
|
|
# :api: plugin
|
|
|
|
def render_to_string(options = {})
|
2009-04-22 03:22:07 -04:00
|
|
|
AbstractController::Renderer.body_to_s(render_to_body(options))
|
2009-04-21 19:02:30 -04:00
|
|
|
end
|
|
|
|
|
2009-03-19 18:45:48 -04:00
|
|
|
def _render_template(template, options)
|
|
|
|
_action_view._render_template_with_layout(template)
|
2009-02-27 14:42:13 -05:00
|
|
|
end
|
2009-03-19 18:45:48 -04:00
|
|
|
|
|
|
|
def view_paths() _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-02-24 20:25:21 -05:00
|
|
|
module ClassMethods
|
2009-03-19 18:45:48 -04:00
|
|
|
|
2009-02-24 20:25:21 -05:00
|
|
|
def append_view_path(path)
|
|
|
|
self.view_paths << path
|
|
|
|
end
|
2009-03-19 18:45:48 -04:00
|
|
|
|
|
|
|
def view_paths
|
|
|
|
self._view_paths
|
|
|
|
end
|
|
|
|
|
|
|
|
def view_paths=(paths)
|
|
|
|
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
|