From c6123c37030b715d088860ea1ca79060659b0e3c Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 20 Mar 2009 16:50:51 -0700 Subject: Finished implementing layout for render :text --- .../lib/action_controller/abstract/renderer.rb | 8 ++++--- .../lib/action_controller/new_base/layouts.rb | 23 +++++++++++++++--- .../lib/action_controller/new_base/renderer.rb | 10 ++++---- .../abstract_controller_test.rb | 8 +++++-- actionpack/test/abstract_controller/helper_test.rb | 4 ++++ actionpack/test/new_base/render_text_test.rb | 28 ++++++++++++++++++++-- actionpack/test/new_base/test_helper.rb | 2 +- 7 files changed, 67 insertions(+), 16 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index d95158be42..5daade6109 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -20,8 +20,8 @@ module AbstractController @_action_view ||= ActionView::Base.new(self.class.view_paths, {}, self) end - def render(name = action_name, options = {}) - self.response_body = render_to_string(name, options) + def render(options = {}) + self.response_body = render_to_string(options) end # Raw rendering of a template. @@ -30,7 +30,9 @@ module AbstractController # @option _layout The relative path to the layout template to use # # :api: plugin - def render_to_string(name = action_name, options = {}) + def render_to_string(options = {}) + name = options[:_template_name] || action_name + template = options[:_template] || view_paths.find_by_parts(name.to_s, formats, options[:_prefix]) _render_template(template, options) end diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb index cdf2224e39..da516c0b85 100644 --- a/actionpack/lib/action_controller/new_base/layouts.rb +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -1,16 +1,33 @@ module ActionController module Layouts def render_to_string(options) - options[:_layout] = options[:layout] || _layout + if !options.key?(:text) || options.key?(:layout) + options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _layout + end + super end + private + + def _layout_for_option(name) + case name + when String then _layout_for_name(name) + when true then _layout + when false then nil + end + end + + def _layout_for_name(name) + view_paths.find_by_parts(name, formats, "layouts") + end + def _layout begin - view_paths.find_by_parts(controller_path, formats, "layouts") + _layout_for_name(controller_path) rescue ActionView::MissingTemplate begin - view_paths.find_by_parts("application", formats, "layouts") + _layout_for_name("application") rescue ActionView::MissingTemplate end end diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index 6abf3cef11..24ca9be077 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -38,13 +38,13 @@ module ActionController options[:_template] = ActionView::TextTemplate.new(_text(options)) template = nil elsif options.key?(:template) - template = options.delete(:template) + options[:_template_name] = options[:template] elsif options.key?(:action) - template = options.delete(:action).to_s + options[:_template_name] = options[:action].to_s options[:_prefix] = _prefix end - super(template, options) + super(options) end private @@ -54,7 +54,7 @@ module ActionController end def _text(options) - text = options.delete(:text) + text = options[:text] case text when nil then " " @@ -63,7 +63,7 @@ module ActionController end def _process_options(options) - if status = options.delete(:status) + if status = options[:status] response.status = status.to_i end end diff --git a/actionpack/test/abstract_controller/abstract_controller_test.rb b/actionpack/test/abstract_controller/abstract_controller_test.rb index 31c28a5c48..96193fd24c 100644 --- a/actionpack/test/abstract_controller/abstract_controller_test.rb +++ b/actionpack/test/abstract_controller/abstract_controller_test.rb @@ -31,7 +31,11 @@ module AbstractController def _prefix() end - def render(name = action_name, options = {}) + def render(options = {}) + if options.is_a?(String) + options = {:_template_name => options} + end + options[:_prefix] = _prefix super end @@ -130,7 +134,7 @@ module AbstractController self.class.layout(formats) end - def render_to_string(name = action_name, options = {}) + def render_to_string(options = {}) options[:_layout] = options[:layout] || _layout super end diff --git a/actionpack/test/abstract_controller/helper_test.rb b/actionpack/test/abstract_controller/helper_test.rb index 81dbee3065..e1b2141331 100644 --- a/actionpack/test/abstract_controller/helper_test.rb +++ b/actionpack/test/abstract_controller/helper_test.rb @@ -7,6 +7,10 @@ module AbstractController include Renderer include Helpers + def render(string) + super(:_template_name => string) + end + append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) end diff --git a/actionpack/test/new_base/render_text_test.rb b/actionpack/test/new_base/render_text_test.rb index 61ec6e05df..f91e6bd644 100644 --- a/actionpack/test/new_base/render_text_test.rb +++ b/actionpack/test/new_base/render_text_test.rb @@ -8,7 +8,7 @@ module HappyPath "layouts/greetings.html.erb" => "<%= yield %>, I wish thee well." )] - def render_hello_world_from_variable + def render_hello_world render :text => "hello david" end @@ -36,6 +36,14 @@ module HappyPath render :text => "hello world", :layout => true end + def render_text_with_layout_false + render :text => "hello world", :layout => false + end + + def render_text_with_layout_nil + render :text => "hello world", :layout => nil + end + def render_text_with_custom_layout render :text => "hello world", :layout => "greetings" end @@ -44,7 +52,7 @@ module HappyPath class TestSimpleTextRender < SimpleRouteCase describe "Rendering text from a action with default options renders the text without the layout" - get "/happy_path/render_text/render_hello_world_from_variable" + get "/happy_path/render_text/render_hello_world" assert_body "hello david" assert_status 200 end @@ -96,4 +104,20 @@ module HappyPath assert_body "hello world, I wish thee well." assert_status 200 end + + class TestTextRenderWithLayoutFalse < SimpleRouteCase + describe "Rendering text with :layout => false" + + get "/happy_path/render_text/render_text_with_layout_false" + assert_body "hello world" + assert_status 200 + end + + class TestTextRenderWithLayoutNil < SimpleRouteCase + describe "Rendering text with :layout => nil" + + get "/happy_path/render_text/render_text_with_layout_nil" + assert_body "hello world" + assert_status 200 + end end \ No newline at end of file diff --git a/actionpack/test/new_base/test_helper.rb b/actionpack/test/new_base/test_helper.rb index affb52a432..a66fc321e6 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -32,8 +32,8 @@ module ActionController include ActionController::HideActions include ActionController::UrlFor - include ActionController::Renderer include ActionController::Layouts + include ActionController::Renderer def self.inherited(klass) @subclasses ||= [] -- cgit v1.2.3