From bcc1930e94583ea27def42ccf306a06eb0623935 Mon Sep 17 00:00:00 2001 From: Dorren Chen Date: Sun, 13 Jun 2010 22:20:11 -0400 Subject: [PATCH] #4855. fix layout inheritance --- actionpack/lib/action_controller/layout.rb | 7 +++- actionpack/test/controller/layout_test.rb | 45 ++++++++++++++++++++ .../test/fixtures/layout_tests/views/goodbye.rhtml | 1 + 3 files changed, 52 insertions(+), 1 deletions(-) create mode 100644 actionpack/test/fixtures/layout_tests/views/goodbye.rhtml diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index 73fafa6..6890834 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -164,6 +164,8 @@ module ActionController #:nodoc: # performance and have access to them as any normal template would. def layout(template_name, conditions = {}, auto = false) add_layout_conditions(conditions) + prev_layout = read_inheritable_attribute(:layout) + write_inheritable_attribute(:previous_layout, prev_layout) write_inheritable_attribute(:layout, template_name) write_inheritable_attribute(:auto_layout, auto) end @@ -237,7 +239,10 @@ module ActionController #:nodoc: when FalseClass nil when NilClass, TrueClass - active_layout if action_has_layout? && candidate_for_layout?(:template => default_template_name) + # active_layout if action_has_layout? && candidate_for_layout?(:template => default_template_name) + if candidate_for_layout?(:template => default_template_name) + action_has_layout? ? active_layout : active_layout(self.class.read_inheritable_attribute(:previous_layout)) + end else active_layout(layout, :html_fallback => true) end diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 7bed963..9eebe81 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -213,3 +213,48 @@ unless RUBY_PLATFORM =~ /(:?mswin|mingw|bccwin)/ end end +class LayoutWithOnly < LayoutTest + layout "item", :only => :hello + + def hello; end + def goodbye; end +end + +class LayoutWithExcept < LayoutTest + layout "item", :except => :hello + + def hello; end + def goodbye; end +end + +class ChildLayout < LayoutWithOnly +end + +class ConditionalLayoutTest < ActionController::TestCase + def test_layout_with_only + @controller = LayoutWithOnly.new + get :hello + assert_equal 'layouts/item', @response.layout + + get :goodbye + assert_equal 'layouts/layout_test', @response.layout + end + + def test_layout_with_except + @controller = LayoutWithExcept.new + get :hello + assert_equal 'layouts/layout_test', @response.layout + + get :goodbye + assert_equal 'layouts/item', @response.layout + end + + def test_inherited_layout_with_options + @controller = ChildLayout.new + get :hello + assert_equal 'layouts/item', @response.layout + + get :goodbye + assert_equal 'layouts/layout_test', @response.layout + end +end \ No newline at end of file diff --git a/actionpack/test/fixtures/layout_tests/views/goodbye.rhtml b/actionpack/test/fixtures/layout_tests/views/goodbye.rhtml new file mode 100644 index 0000000..8fe53d9 --- /dev/null +++ b/actionpack/test/fixtures/layout_tests/views/goodbye.rhtml @@ -0,0 +1 @@ +goodbye.rhtml \ No newline at end of file -- 1.6.4.4