From 48e564efcb7f8d7a0d08ce266aba167c8f5e2ab3 Mon Sep 17 00:00:00 2001 From: Federico Brubacher Date: Sat, 8 May 2010 16:32:14 -0300 Subject: [PATCH 1/2] added test for actions in a condition, and a failing test when an action doesn't match the condition it should render parents layout --- actionpack/test/abstract/layouts_test.rb | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-) diff --git a/actionpack/test/abstract/layouts_test.rb b/actionpack/test/abstract/layouts_test.rb index f580ad4..86f77d2 100644 --- a/actionpack/test/abstract/layouts_test.rb +++ b/actionpack/test/abstract/layouts_test.rb @@ -11,6 +11,7 @@ module AbstractControllerTests self.view_paths = [ActionView::FixtureResolver.new( "layouts/hello.erb" => "With String <%= yield %>", "layouts/hello_override.erb" => "With Override <%= yield %>", + "layouts/will_only_show_on_index.erb" => "I'm in index <%= yield %>", "layouts/overwrite.erb" => "Overwrite <%= yield %>", "layouts/with_false_layout.erb" => "False Layout <%= yield %>", "abstract_controller_tests/layouts/with_string_implied_child.erb" => @@ -57,6 +58,13 @@ module AbstractControllerTests layout "hello_override" end + class WithStringOverriddenChildWithCondition < WithString + layout "will_only_show_on_index", :only => :index + def new + render :template => ActionView::Template::Text.new("Hello string!") + end + end + class WithNilChild < WithString layout nil end @@ -244,6 +252,18 @@ module AbstractControllerTests assert_equal "With Override Hello string!", controller.response_body end + test "when a child controller has specified a layout with a condition, use that layout and not the parent controller in the specified conditon action" do + controller = WithStringOverriddenChildWithCondition.new + controller.process(:index) + assert_equal "I'm in index Hello string!", controller.response_body + end + + test "when a child controller has specified a layout with a condition, and the action does not fit in the condition, it should render the parent action" do + controller = WithStringOverriddenChildWithCondition.new + controller.process(:new) + assert_equal "With String Hello string!", controller.response_body + end + test "when a child controller has an implied layout, use that layout and not the parent controller layout" do controller = WithStringImpliedChild.new controller.process(:index) -- 1.7.0.5 From 2a550a7b6c2121d702facde25eb00c0e41ca5d47 Mon Sep 17 00:00:00 2001 From: Federico Brubacher Date: Sat, 8 May 2010 17:12:39 -0300 Subject: [PATCH 2/2] layout fallbacks to a parent layout for the actions that not match the conditions --- actionpack/lib/abstract_controller/layouts.rb | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb index 319472c..09928ee 100644 --- a/actionpack/lib/abstract_controller/layouts.rb +++ b/actionpack/lib/abstract_controller/layouts.rb @@ -172,6 +172,7 @@ module AbstractController module ClassMethods def inherited(klass) super + klass.instance_variable_set(:@parent_layout, @_layout) klass._write_layout_method end @@ -343,7 +344,11 @@ module AbstractController # Template:: The template object for the default layout (or nil) def _default_layout(require_layout = false) begin - layout_name = _layout if action_has_layout? + if action_has_layout? + layout_name = _layout + else + layout_name = self.class.instance_variable_get(:@parent_layout) + end rescue NameError => e raise NoMethodError, "You specified #{@_layout.inspect} as the layout, but no such method was found" -- 1.7.0.5