From c39acc7fbc4094fccfba6b71d7c023bd85dd4f7d Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Fri, 20 Feb 2009 15:23:16 +0100 Subject: [PATCH] make action_controller/layouts pick templates from the current instance's view_paths instead of the class view_paths --- actionpack/lib/action_controller/layout.rb | 58 +++++++++----------- actionpack/test/controller/layout_test.rb | 14 +++++ 2 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml diff --git a/actionpack/lib/action_controller/layout.rb b/actionpack/lib/action_controller/layout.rb index 183d56c..a6ea482 100644 --- a/actionpack/lib/action_controller/layout.rb +++ b/actionpack/lib/action_controller/layout.rb @@ -172,23 +172,10 @@ module ActionController #:nodoc: @layout_conditions ||= read_inheritable_attribute(:layout_conditions) end - def default_layout(format) #:nodoc: - layout = read_inheritable_attribute(:layout) - return layout unless read_inheritable_attribute(:auto_layout) - find_layout(layout, format) - end - def layout_list #:nodoc: Array(view_paths).sum([]) { |path| Dir["#{path.to_str}/layouts/**/*"] } end - def find_layout(layout, *formats) #:nodoc: - return layout if layout.respond_to?(:render) - view_paths.find_template(layout.to_s =~ /layouts\// ? layout : "layouts/#{layout}", *formats) - rescue ActionView::MissingTemplate - nil - end - private def inherited_with_layout(child) inherited_without_layout(child) @@ -212,35 +199,29 @@ module ActionController #:nodoc: # object). If the layout was defined without a directory, layouts is assumed. So layout "weblog/standard" will return # weblog/standard, but layout "standard" will return layouts/standard. def active_layout(passed_layout = nil) - layout = passed_layout || self.class.default_layout(default_template_format) + layout = passed_layout || default_layout + return layout if layout.respond_to?(:render) active_layout = case layout when Symbol then __send__(layout) when Proc then layout.call(self) else layout end - - if active_layout - if layout = self.class.find_layout(active_layout, @template.template_format) - layout - else - raise ActionView::MissingTemplate.new(self.class.view_paths, active_layout) - end - end + + find_layout(active_layout, @template.template_format) if active_layout end private - def candidate_for_layout?(options) - template = options[:template] || default_template(options[:action]) - if options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty? - begin - !self.view_paths.find_template(template, default_template_format).exempt_from_layout? - rescue ActionView::MissingTemplate - true - end - end + def default_layout #:nodoc: + layout = self.class.read_inheritable_attribute(:layout) + return layout unless self.class.read_inheritable_attribute(:auto_layout) + find_layout(layout, default_template_format) rescue ActionView::MissingTemplate - false + nil + end + + def find_layout(layout, *formats) #:nodoc: + view_paths.find_template(layout.to_s =~ /layouts\// ? layout : "layouts/#{layout}", *formats) end def pick_layout(options) @@ -273,6 +254,19 @@ module ActionController #:nodoc: end end + def candidate_for_layout?(options) + template = options[:template] || default_template(options[:action]) + if options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty? + begin + !self.view_paths.find_template(template, default_template_format).exempt_from_layout? + rescue ActionView::MissingTemplate + true + end + end + rescue ActionView::MissingTemplate + false + end + def default_template_format response.template.template_format end diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb index 2f5e830..28555ee 100644 --- a/actionpack/test/controller/layout_test.rb +++ b/actionpack/test/controller/layout_test.rb @@ -83,6 +83,13 @@ class HasOwnLayoutController < LayoutTest layout 'item' end +class PrependsViewPathController < LayoutTest + def hello + prepend_view_path File.dirname(__FILE__) + '/../fixtures/layout_tests/alt/' + render :layout => 'alt' + end +end + class SetsLayoutInRenderController < LayoutTest def hello render :layout => 'third_party_template_library' @@ -130,6 +137,12 @@ class LayoutSetInResponseTest < ActionController::TestCase ensure ActionController::Base.exempt_from_layout.delete(/\.rhtml$/) end + + def test_layout_is_picked_from_the_controller_instances_view_path + @controller = PrependsViewPathController.new + get :hello + assert_equal 'layouts/alt', @response.layout + end end class RenderWithTemplateOptionController < LayoutTest @@ -178,3 +191,4 @@ unless RUBY_PLATFORM =~ /(:?mswin|mingw|bccwin)/ end end end + diff --git a/actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml b/actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml new file mode 100644 index 0000000..e69de29 -- 1.6.0.5