From 890321e51e0c51f61f9198d247727e98f7485899 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 19 Mar 2009 15:45:48 -0700 Subject: Get very basic layouts working. * Required small architecture change --- .../lib/action_controller/abstract/layouts.rb | 7 +--- .../lib/action_controller/abstract/renderer.rb | 47 +++++++++++++++------- actionpack/lib/action_controller/new_base.rb | 1 + .../lib/action_controller/new_base/layouts.rb | 19 +++++++++ .../lib/action_controller/new_base/renderer.rb | 16 +++++--- 5 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 actionpack/lib/action_controller/new_base/layouts.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/abstract/layouts.rb b/actionpack/lib/action_controller/abstract/layouts.rb index c8d6e77fce..c6b99a6d45 100644 --- a/actionpack/lib/action_controller/abstract/layouts.rb +++ b/actionpack/lib/action_controller/abstract/layouts.rb @@ -1,10 +1,7 @@ module AbstractController module Layouts - def _render_template(tmp) - _action_view._render_template_with_layout(tmp, _layout) - end - - def _layout + def _render_template(template, options) + _action_view._render_template_with_layout(template, options[:_layout]) end end end \ No newline at end of file diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index 19a64b0c38..ad996d0daf 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -4,12 +4,14 @@ module AbstractController module Renderer def self.included(klass) - klass.class_eval do + klass.class_eval do extend ClassMethods + attr_internal :formats - - extlib_inheritable_accessor :view_paths - self.view_paths ||= ActionView::PathSet.new + + extlib_inheritable_accessor :_view_paths + + self._view_paths ||= ActionView::PathSet.new include AbstractController::Logger end end @@ -17,27 +19,42 @@ module AbstractController def _action_view @_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self) end - - def _prefix - end - - def render(template = action_name) - self.response_body = render_to_string(template) + + def render(name = action_name, options = {}) + self.response_body = render_to_string(name, options) end - def render_to_string(template = action_name, prefix = true) - tmp = view_paths.find_by_parts(template.to_s, formats, (_prefix if prefix)) - _render_template(tmp) + # Raw rendering of a template. + # ==== + # @option _prefix The template's path prefix + # @option _layout The relative path to the layout template to use + # + # :api: plugin + def render_to_string(name = action_name, options = {}) + template = view_paths.find_by_parts(name.to_s, formats, options[:_prefix]) + _render_template(template, options) end - def _render_template(tmp) - _action_view._render_template_with_layout(tmp) + def _render_template(template, options) + _action_view._render_template_with_layout(template) end + + def view_paths() _view_paths end module ClassMethods + def append_view_path(path) self.view_paths << path end + + 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 end end end \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base.rb b/actionpack/lib/action_controller/new_base.rb index 2870f71b7d..7c65f1cdc1 100644 --- a/actionpack/lib/action_controller/new_base.rb +++ b/actionpack/lib/action_controller/new_base.rb @@ -1,6 +1,7 @@ module ActionController autoload :AbstractBase, "action_controller/new_base/base" autoload :HideActions, "action_controller/new_base/hide_actions" + autoload :Layouts, "action_controller/new_base/layouts" autoload :Renderer, "action_controller/new_base/renderer" autoload :UrlFor, "action_controller/new_base/url_for" end \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb new file mode 100644 index 0000000000..cdf2224e39 --- /dev/null +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -0,0 +1,19 @@ +module ActionController + module Layouts + def render_to_string(options) + options[:_layout] = options[:layout] || _layout + super + end + + def _layout + begin + view_paths.find_by_parts(controller_path, formats, "layouts") + rescue ActionView::MissingTemplate + begin + view_paths.find_by_parts("application", formats, "layouts") + rescue ActionView::MissingTemplate + end + end + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index 540924936d..1308537160 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -11,6 +11,11 @@ module ActionController # end # end + def initialize(*) + self.formats = [:html] + super + end + def render(action, options = {}) # TODO: Move this into #render_to_string if action.is_a?(Hash) @@ -23,22 +28,21 @@ module ActionController self.response_body = render_to_string(options) end - - def render_to_string(options) - self.formats = [:html] + def render_to_string(options) unless options.is_a?(Hash) options = {:action => options} end - + if options.key?(:text) _render_text(options) elsif options.key?(:template) template = options.delete(:template) - super(template, false) + super(template) elsif options.key?(:action) template = options.delete(:action).to_s - super(template) + options[:_prefix] = _prefix + super(template, options) end end -- cgit v1.2.3