From 34f058e082754bb726a4753fa26e8e8c082702c0 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 23 Mar 2009 12:07:34 -0700 Subject: Add a bunch of tests for various render :action, layout combinations --- .../lib/action_controller/new_base/layouts.rb | 9 +- actionpack/lib/action_view/paths.rb | 3 + actionpack/test/new_base/fixture_view_path_test.rb | 60 ----- actionpack/test/new_base/render_action_test.rb | 281 +++++++++++++++++---- actionpack/test/new_base/test_helper.rb | 2 +- 5 files changed, 242 insertions(+), 113 deletions(-) delete mode 100644 actionpack/test/new_base/fixture_view_path_test.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb index da516c0b85..3b47a76077 100644 --- a/actionpack/lib/action_controller/new_base/layouts.rb +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -2,7 +2,7 @@ module ActionController module Layouts def render_to_string(options) if !options.key?(:text) || options.key?(:layout) - options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _layout + options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout end super @@ -13,7 +13,7 @@ module ActionController def _layout_for_option(name) case name when String then _layout_for_name(name) - when true then _layout + when true then _default_layout(true) when false then nil end end @@ -22,13 +22,14 @@ module ActionController view_paths.find_by_parts(name, formats, "layouts") end - def _layout + def _default_layout(require_layout = false) begin _layout_for_name(controller_path) rescue ActionView::MissingTemplate begin _layout_for_name("application") - rescue ActionView::MissingTemplate + rescue ActionView::MissingTemplate => e + raise e if require_layout end end end diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb index d88296daa6..2a1ba3e895 100644 --- a/actionpack/lib/action_view/paths.rb +++ b/actionpack/lib/action_view/paths.rb @@ -42,6 +42,9 @@ module ActionView #:nodoc: end Template.new(path, self) + rescue ActionView::MissingTemplate => e + extension ||= [] + raise ActionView::MissingTemplate.new(self, "#{prefix}/#{path}.{#{extension.join(",")}}") end def find_template(original_template_path, format = nil) diff --git a/actionpack/test/new_base/fixture_view_path_test.rb b/actionpack/test/new_base/fixture_view_path_test.rb deleted file mode 100644 index 7792a14a20..0000000000 --- a/actionpack/test/new_base/fixture_view_path_test.rb +++ /dev/null @@ -1,60 +0,0 @@ -require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") - -module HappyPath - - # This has no layout and it works - class RenderActionLolController < ActionController::Base2 - - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ - "happy_path/render_action_lol/hello_world.html.erb" => "Hello world!", - "happy_path/render_action_lol/goodbye_world.html.erb" => "Goodbye world!", - "happy_path/sexy_time/borat.html.erb" => "I LIKE!!!" - })] - - def render_action_hello_world - render :action => "hello_world" - end - - def render_action_goodbye_world - render :action => "goodbye_world" - end - - end - - class SexyTimeController < ActionController::Base2 - self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ - "happy_path/render_action_lol/hello_world.html.erb" => "Hello world!", - "happy_path/render_action_lol/goodbye_world.html.erb" => "Goodbye world!", - "happy_path/sexy_time/borat.html.erb" => "I LIKE!!!" - })] - - def borat - render "borat" - end - end - - class TestRenderHelloAction < SimpleRouteCase - - describe "Rendering an action using :action => " - - get "/happy_path/render_action/render_action_hello_world" - assert_body "Hello world!" - assert_status 200 - - end - - class TestRenderGoodbyeAction < SimpleRouteCase - describe "Goodbye" - - get "/happy_path/render_action_lol/render_action_goodbye_world" - assert_body "Goodbye world!" - assert_status 200 - end - - class TestRenderBorat < SimpleRouteCase - describe "Borat yo" - get "/happy_path/sexy_time/borat" - assert_body "I LIKE!!!" - assert_status 200 - end -end \ No newline at end of file diff --git a/actionpack/test/new_base/render_action_test.rb b/actionpack/test/new_base/render_action_test.rb index 99dbfb878b..bbc8939af4 100644 --- a/actionpack/test/new_base/render_action_test.rb +++ b/actionpack/test/new_base/render_action_test.rb @@ -1,138 +1,323 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") -module HappyPath +module RenderAction # This has no layout and it works - class RenderActionController < ActionController::Base2 + class BasicController < ActionController::Base2 self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( - "happy_path/render_action/hello_world.html.erb" => "Hello world!" + "render_action/basic/hello_world.html.erb" => "Hello world!" )] - def render_action_hello_world + def hello_world render :action => "hello_world" end - def render_action_hello_world_as_string + def hello_world_as_string render "hello_world" end - def render_action_hello_world_as_string_with_options + def hello_world_as_string_with_options render "hello_world", :status => 404 end - def render_action_hello_world_as_symbol + def hello_world_as_symbol render :hello_world end - def render_action_hello_world_with_symbol + def hello_world_with_symbol render :action => :hello_world end + def hello_world_with_layout + render :action => "hello_world", :layout => true + end + + def hello_world_with_layout_false + render :action => "hello_world", :layout => false + end + + def hello_world_with_layout_nil + render :action => "hello_world", :layout => nil + end + + def hello_world_with_custom_layout + render :action => "hello_world", :layout => "greetings" + end + end - class TestRenderAction < SimpleRouteCase + class TestBasic < SimpleRouteCase describe "Rendering an action using :action => " - get "/happy_path/render_action/render_action_hello_world" + get "/render_action/basic/hello_world" assert_body "Hello world!" assert_status 200 end - class TestRenderActionWithString < SimpleRouteCase + class TestWithString < SimpleRouteCase describe "Render an action using 'hello_world'" - get "/happy_path/render_action/render_action_hello_world_as_string" + get "/render_action/basic/hello_world_as_string" assert_body "Hello world!" assert_status 200 end - class TestRenderActionWithStringAndOptions < SimpleRouteCase + class TestWithStringAndOptions < SimpleRouteCase describe "Render an action using 'hello_world'" - get "/happy_path/render_action/render_action_hello_world_as_string_with_options" + get "/render_action/basic/hello_world_as_string_with_options" assert_body "Hello world!" assert_status 404 end - class TestRenderActionAsSymbol < SimpleRouteCase + class TestAsSymbol < SimpleRouteCase describe "Render an action using :hello_world" - get "/happy_path/render_action/render_action_hello_world_as_symbol" + get "/render_action/basic/hello_world_as_symbol" assert_body "Hello world!" assert_status 200 end - class TestRenderActionWithSymbol < SimpleRouteCase + class TestWithSymbol < SimpleRouteCase describe "Render an action using :action => :hello_world" - get "/happy_path/render_action/render_action_hello_world_with_symbol" + get "/render_action/basic/hello_world_with_symbol" assert_body "Hello world!" assert_status 200 end - # # ==== Render actions with layouts ==== - - class RenderActionWithLayoutController < ActionController::Base2 - # Set the view path to an application view structure with layouts - self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ - "happy_path/render_action_with_layout/hello_world.html.erb" => "Hello World!", - "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI" - })] + class TestLayoutTrue < SimpleRouteCase + describe "rendering a normal template with full path with layout => true" - def hello_world - render :action => "hello_world" + test "raises an exception when requesting a layout and none exist" do + assert_raise(ActionView::MissingTemplate) { get "/render_action/basic/hello_world_with_layout" } end end - class RenderActionWithControllerLayoutController < ActionController::Base2 - self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ - "happy_path/render_action_with_controller_layout/hello_world.html.erb" => "Hello World!", - "layouts/happy_path/render_action_with_controller_layout.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" - })] + class TestLayoutFalse < SimpleRouteCase + describe "rendering a normal template with full path with layout => false" - def hello_world - render :action => "hello_world" + get "/render_action/basic/hello_world_with_layout_false" + assert_body "Hello world!" + assert_status 200 + end + + class TestLayoutNil < SimpleRouteCase + describe "rendering a normal template with full path with layout => :nil" + + get "/render_action/basic/hello_world_with_layout_nil" + assert_body "Hello world!" + assert_status 200 + end + + class TestCustomLayout < SimpleRouteCase + describe "rendering a normal template with full path with layout => 'greetings'" + + test "raises an exception when requesting a layout that does not exist" do + assert_raise(ActionView::MissingTemplate) { get "/render_action/basic/hello_world_with_custom_layout" } end end - class RenderActionWithControllerLayoutFirstController < ActionController::Base2 - self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ - "happy_path/render_action_with_controller_layout_first/hello_world.html.erb" => "Hello World!", - "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", - "layouts/happy_path/render_action_with_controller_layout_first.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" - })] +end + +module RenderActionWithApplicationLayout + + # # ==== Render actions with layouts ==== + + class BasicController < ActionController::Base2 + # Set the view path to an application view structure with layouts + self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!", + "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", + "layouts/greetings.html.erb" => "Greetings <%= yield %> Bai" + )] def hello_world render :action => "hello_world" end + + def hello_world_with_layout + render :action => "hello_world", :layout => true + end + + def hello_world_with_layout_false + render :action => "hello_world", :layout => false + end + + def hello_world_with_layout_nil + render :action => "hello_world", :layout => nil + end + + def hello_world_with_custom_layout + render :action => "hello_world", :layout => "greetings" + end end - class TestRenderActionWithLayout < SimpleRouteCase + class TestDefaultLayout < SimpleRouteCase describe %( Render hello_world and implicitly use application.html.erb as a layout if no layout is specified and no controller layout is present ) - get "/happy_path/render_action_with_layout/hello_world" + get "/render_action_with_application_layout/basic/hello_world" + assert_body "OHAI Hello World! KTHXBAI" + assert_status 200 + end + + class TestLayoutTrue < SimpleRouteCase + describe "rendering a normal template with full path with layout => true" + + get "/render_action_with_application_layout/basic/hello_world_with_layout" assert_body "OHAI Hello World! KTHXBAI" assert_status 200 end - class TestRenderActionWithControllerLayout < SimpleRouteCase + class TestLayoutFalse < SimpleRouteCase + describe "rendering a normal template with full path with layout => false" + + get "/render_action_with_application_layout/basic/hello_world_with_layout_false" + assert_body "Hello World!" + assert_status 200 + end + + class TestLayoutNil < SimpleRouteCase + describe "rendering a normal template with full path with layout => :nil" + + get "/render_action_with_application_layout/basic/hello_world_with_layout_nil" + assert_body "Hello World!" + assert_status 200 + end + + class TestCustomLayout < SimpleRouteCase + describe "rendering a normal template with full path with layout => 'greetings'" + + get "/render_action_with_application_layout/basic/hello_world_with_custom_layout" + assert_body "Greetings Hello World! Bai" + assert_status 200 + end + +end + +module RenderActionWithControllerLayout + + class BasicController < ActionController::Base2 + self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + "render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!", + "layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" + )] + + def hello_world + render :action => "hello_world" + end + + def hello_world_with_layout + render :action => "hello_world", :layout => true + end + + def hello_world_with_layout_false + render :action => "hello_world", :layout => false + end + + def hello_world_with_layout_nil + render :action => "hello_world", :layout => nil + end + + def hello_world_with_custom_layout + render :action => "hello_world", :layout => "greetings" + end + end + + class TestControllerLayout < SimpleRouteCase describe "Render hello_world and implicitly use .html.erb as a layout." - - get "/happy_path/render_action_with_controller_layout/hello_world" + + get "/render_action_with_controller_layout/basic/hello_world" assert_body "With Controller Layout! Hello World! KTHXBAI" assert_status 200 end - class TestRenderActionWithControllerLayoutFirst < SimpleRouteCase + class TestLayoutTrue < SimpleRouteCase + describe "rendering a normal template with full path with layout => true" + + get "/render_action_with_controller_layout/basic/hello_world_with_layout" + assert_body "With Controller Layout! Hello World! KTHXBAI" + assert_status 200 + end + + class TestLayoutFalse < SimpleRouteCase + describe "rendering a normal template with full path with layout => false" + + get "/render_action_with_controller_layout/basic/hello_world_with_layout_false" + assert_body "Hello World!" + assert_status 200 + end + + class TestLayoutNil < SimpleRouteCase + describe "rendering a normal template with full path with layout => :nil" + + get "/render_action_with_controller_layout/basic/hello_world_with_layout_nil" + assert_body "Hello World!" + assert_status 200 + end + +end + +module RenderActionWithBothLayouts + + class BasicController < ActionController::Base2 + self.view_paths = [ActionView::FixtureTemplate::FixturePath.new({ + "render_action_with_both_layouts/basic/hello_world.html.erb" => "Hello World!", + "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", + "layouts/render_action_with_both_layouts/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" + })] + + def hello_world + render :action => "hello_world" + end + + def hello_world_with_layout + render :action => "hello_world", :layout => true + end + + def hello_world_with_layout_false + render :action => "hello_world", :layout => false + end + + def hello_world_with_layout_nil + render :action => "hello_world", :layout => nil + end + end + + class TestControllerLayoutFirst < SimpleRouteCase describe "Render hello_world and implicitly use .html.erb over application.html.erb as a layout" + + get "/render_action_with_both_layouts/basic/hello_world" + assert_body "With Controller Layout! Hello World! KTHXBAI" + assert_status 200 + end + + class TestLayoutTrue < SimpleRouteCase + describe "rendering a normal template with full path with layout => true" - get "/happy_path/render_action_with_controller_layout_first/hello_world" + get "/render_action_with_both_layouts/basic/hello_world_with_layout" assert_body "With Controller Layout! Hello World! KTHXBAI" assert_status 200 end + class TestLayoutFalse < SimpleRouteCase + describe "rendering a normal template with full path with layout => false" + + get "/render_action_with_both_layouts/basic/hello_world_with_layout_false" + assert_body "Hello World!" + assert_status 200 + end + + class TestLayoutNil < SimpleRouteCase + describe "rendering a normal template with full path with layout => :nil" + + get "/render_action_with_both_layouts/basic/hello_world_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 969ae56f05..bb4667007b 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -60,7 +60,7 @@ module ActionView #:nodoc: @hash[k.sub(/\.\w+$/, '')] = FixtureTemplate.new(v, k.split("/").last, self) end - super("") + super("fixtures://root") end def find_template(path) -- cgit v1.2.3