From 95c9718118bc0342ddb320f23b5e0a17fb12b7ad Mon Sep 17 00:00:00 2001 From: Yehuda Katz and Carl Lerche Date: Tue, 7 Apr 2009 15:54:02 -0700 Subject: Layouts work in AbstractController. Add support for the rspec runner for T::U --- .../lib/action_controller/abstract/layouts.rb | 24 +++-- .../test/abstract_controller/layouts_test.rb | 119 ++++++++++++++++----- actionpack/test/new_base/test_helper.rb | 19 +--- 3 files changed, 111 insertions(+), 51 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/abstract/layouts.rb b/actionpack/lib/action_controller/abstract/layouts.rb index 2680283151..1990941916 100644 --- a/actionpack/lib/action_controller/abstract/layouts.rb +++ b/actionpack/lib/action_controller/abstract/layouts.rb @@ -13,6 +13,10 @@ module AbstractController _write_layout_method end + def _implied_layout_name + name.underscore + end + def _write_layout_method case @_layout when String @@ -24,8 +28,8 @@ module AbstractController else self.class_eval %{ def _layout - if view_paths.find_by_parts?("#{controller_path}", formats, "layouts") - "#{controller_path}" + if view_paths.find_by_parts?("#{_implied_layout_name}", formats, "layouts") + "#{_implied_layout_name}" else super end @@ -45,9 +49,12 @@ module AbstractController def _layout_for_option(name) case name - when String then _layout_for_name(name) - when true then _default_layout(true) - when false then nil + when String then _layout_for_name(name) + when true then _default_layout(true) + when false, nil then nil + else + raise ArgumentError, + "String, true, or false, expected for `layout'; you passed #{name.inspect}" end end @@ -56,7 +63,12 @@ module AbstractController end def _default_layout(require_layout = false) - _layout_for_option(_layout) + begin + _layout_for_option(_layout) + rescue NameError => e + raise NoMethodError, + "You specified #{@_layout.inspect} as the layout, but no such method was found" + end end end end \ No newline at end of file diff --git a/actionpack/test/abstract_controller/layouts_test.rb b/actionpack/test/abstract_controller/layouts_test.rb index ec8faffc51..541d126958 100644 --- a/actionpack/test/abstract_controller/layouts_test.rb +++ b/actionpack/test/abstract_controller/layouts_test.rb @@ -9,9 +9,12 @@ module AbstractControllerTests use AbstractController::Layouts self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( - "layouts/hello.erb" => "With String <%= yield %>", - "layouts/omg.erb" => "OMGHI2U <%= yield %>", - "layouts/with_false_layout.erb" => "False Layout <%= yield %>" + "layouts/hello.erb" => "With String <%= yield %>", + "layouts/hello_override.erb" => "With Override <%= yield %>", + "layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" => + "With Implied <%= yield %>", + "layouts/omg.erb" => "OMGHI2U <%= yield %>", + "layouts/with_false_layout.erb" => "False Layout <%= yield %>" )] def self.controller_path @@ -42,6 +45,23 @@ module AbstractControllerTests end end + class WithStringChild < WithString + end + + class WithStringOverriddenChild < WithString + layout "hello_override" + end + + class WithNilChild < WithString + layout nil + end + + class WithStringImpliedChild < WithString + end + + class WithChildOfImplied < WithStringImpliedChild + end + class WithSymbol < Base layout :hello @@ -66,6 +86,36 @@ module AbstractControllerTests end end + class WithSymbolReturningNil < Base + layout :nilz + + def index + render :_template => ActionView::TextTemplate.new("Hello nilz!") + end + + def nilz() end + end + + class WithSymbolReturningObj < Base + layout :objekt + + def index + render :_template => ActionView::TextTemplate.new("Hello nilz!") + end + + def objekt + Object.new + end + end + + class WithSymbolAndNoMethod < Base + layout :omg_no_method + + def index + render :_template => ActionView::TextTemplate.new("Hello boom!") + end + end + class WithMissingLayout < Base layout "missing" @@ -82,6 +132,24 @@ module AbstractControllerTests end end + class WithNilLayout < Base + layout nil + + def index + render :_template => ActionView::TextTemplate.new("Hello nil!") + end + end + + # TODO Move to bootloader + AbstractController::Base.subclasses.each do |klass| + klass = klass.constantize + next unless klass < AbstractController::Layouts + p klass + klass.class_eval do + _write_layout_method + end + end + class TestBase < ActiveSupport::TestCase test "when no layout is specified, and no default is available, render without a layout" do result = Blank.process(:index) @@ -103,7 +171,8 @@ module AbstractControllerTests end test "when layout is specified as nil, do not use a layout" do - pending + result = WithNilLayout.process(:index) + assert_equal "Hello nil!", result.response_obj[:body] end test "when layout is specified as a symbol, call the requested method and use the layout returned" do @@ -112,47 +181,43 @@ module AbstractControllerTests end test "when layout is specified as a symbol and the method returns nil, don't use a layout" do - pending + result = WithSymbolReturningNil.process(:index) + assert_equal "Hello nilz!", result.response_obj[:body] end test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do - pending + assert_raises(NoMethodError, /:nilz/) { WithSymbolAndNoMethod.process(:index) } end test "when the layout is specified as a symbol and the method returns something besides a string/false/nil, raise an exception" do - pending + assert_raises(ArgumentError) { WithSymbolReturningObj.process(:index) } end test "when a child controller does not have a layout, use the parent controller layout" do - pending + result = WithStringChild.process(:index) + assert_equal "With String Hello string!", result.response_obj[:body] end test "when a child controller has specified a layout, use that layout and not the parent controller layout" do - pending + result = WithStringOverriddenChild.process(:index) + assert_equal "With Override Hello string!", result.response_obj[:body] end test "when a child controller has an implied layout, use that layout and not the parent controller layout" do - pending + result = WithStringImpliedChild.process(:index) + assert_equal "With Implied Hello string!", result.response_obj[:body] end test "when a child controller specifies layout nil, do not use the parent layout" do - pending - end - - test "when a child controller has an implied layout, use that layout instead of the parent controller layout" do - pending - end - - test %( - when a grandchild has no layout specified, the child has an implied layout, and the - parent has specified a layout, use the child controller layout - ) do - pending - end - - test "Raise ArgumentError if layout is called with a bad argument" do - pending - end + result = WithNilChild.process(:index) + assert_equal "Hello string!", result.response_obj[:body] + end + + test "when a grandchild has no layout specified, the child has an implied layout, and the " \ + "parent has specified a layout, use the child controller layout" do + result = WithChildOfImplied.process(:index) + assert_equal "With Implied Hello string!", result.response_obj[:body] + end end 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 9058060059..5edd7b4f63 100644 --- a/actionpack/test/new_base/test_helper.rb +++ b/actionpack/test/new_base/test_helper.rb @@ -70,24 +70,7 @@ class Rack::TestCase < ActiveSupport::TestCase ActionController::Routing.use_controllers!(controllers) end - - unless method_defined?(:describe) - def self.describe(text) - class_eval <<-RUBY_EVAL - def self.name - "#{text}" - end - RUBY_EVAL - end - end - - if defined?(Spec) - class << self - undef test - alias_method :test, :it - end - end - + def app @app ||= ActionController::Dispatcher.new end -- cgit v1.2.3