Skip to content
This repository
Browse code

make action_controller/layouts pick templates from the current instan…

…ce's view_paths instead of the class view_paths [#1974 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit 8c5cc66a831aadb159f3daaffa4208064c30af0e 1 parent 01c818e
Sven Fuchs authored February 20, 2009 josh committed February 20, 2009
58  actionpack/lib/action_controller/layout.rb
@@ -172,23 +172,10 @@ def layout_conditions #:nodoc:
172 172
         @layout_conditions ||= read_inheritable_attribute(:layout_conditions)
173 173
       end
174 174
 
175  
-      def default_layout(format) #:nodoc:
176  
-        layout = read_inheritable_attribute(:layout) unless format == :js
177  
-        return layout unless read_inheritable_attribute(:auto_layout)
178  
-        find_layout(layout, format)
179  
-      end
180  
-
181 175
       def layout_list #:nodoc:
182 176
         Array(view_paths).sum([]) { |path| Dir["#{path.to_str}/layouts/**/*"] }
183 177
       end
184 178
 
185  
-      def find_layout(layout, *formats) #:nodoc:
186  
-        return layout if layout.respond_to?(:render)
187  
-        view_paths.find_template(layout.to_s =~ /layouts\// ? layout : "layouts/#{layout}", *formats)
188  
-      rescue ActionView::MissingTemplate
189  
-        nil
190  
-      end
191  
-
192 179
       private
193 180
         def inherited_with_layout(child)
194 181
           inherited_without_layout(child)
@@ -212,35 +199,29 @@ def normalize_conditions(conditions)
212 199
     # object). If the layout was defined without a directory, layouts is assumed. So <tt>layout "weblog/standard"</tt> will return
213 200
     # weblog/standard, but <tt>layout "standard"</tt> will return layouts/standard.
214 201
     def active_layout(passed_layout = nil)
215  
-      layout = passed_layout || self.class.default_layout(default_template_format)
  202
+      layout = passed_layout || default_layout
  203
+      return layout if layout.respond_to?(:render)
216 204
 
217 205
       active_layout = case layout
218 206
         when Symbol then __send__(layout)
219 207
         when Proc   then layout.call(self)
220 208
         else layout
221 209
       end
222  
-
223  
-      if active_layout
224  
-        if layout = self.class.find_layout(active_layout, @template.template_format)
225  
-          layout
226  
-        else
227  
-          raise ActionView::MissingTemplate.new(self.class.view_paths, active_layout)
228  
-        end
229  
-      end
  210
+      
  211
+      find_layout(active_layout, @template.template_format) if active_layout
230 212
     end
231 213
 
232 214
     private
233  
-      def candidate_for_layout?(options)
234  
-        template = options[:template] || default_template(options[:action])
235  
-        if options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty?
236  
-          begin
237  
-            !self.view_paths.find_template(template, default_template_format).exempt_from_layout?
238  
-          rescue ActionView::MissingTemplate
239  
-            true
240  
-          end
241  
-        end
  215
+      def default_layout #:nodoc:
  216
+        layout = self.class.read_inheritable_attribute(:layout) unless default_template_format == :js
  217
+        return layout unless self.class.read_inheritable_attribute(:auto_layout)
  218
+        find_layout(layout, default_template_format)
242 219
       rescue ActionView::MissingTemplate
243  
-        false
  220
+        nil
  221
+      end
  222
+
  223
+      def find_layout(layout, *formats) #:nodoc:
  224
+        view_paths.find_template(layout.to_s =~ /layouts\// ? layout : "layouts/#{layout}", *formats)
244 225
       end
245 226
 
246 227
       def pick_layout(options)
@@ -273,6 +254,19 @@ def action_has_layout?
273 254
         end
274 255
       end
275 256
 
  257
+      def candidate_for_layout?(options)
  258
+        template = options[:template] || default_template(options[:action])
  259
+        if options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty?
  260
+          begin
  261
+            !self.view_paths.find_template(template, default_template_format).exempt_from_layout?
  262
+          rescue ActionView::MissingTemplate
  263
+            true
  264
+          end
  265
+        end
  266
+      rescue ActionView::MissingTemplate
  267
+        false
  268
+      end
  269
+
276 270
       def default_template_format
277 271
         response.template.template_format
278 272
       end
14  actionpack/test/controller/layout_test.rb
@@ -83,6 +83,13 @@ class HasOwnLayoutController < LayoutTest
83 83
   layout 'item'
84 84
 end
85 85
 
  86
+class PrependsViewPathController < LayoutTest
  87
+  def hello
  88
+    prepend_view_path File.dirname(__FILE__) + '/../fixtures/layout_tests/alt/'
  89
+    render :layout => 'alt'
  90
+  end
  91
+end
  92
+
86 93
 class SetsLayoutInRenderController < LayoutTest
87 94
   def hello
88 95
     render :layout => 'third_party_template_library'
@@ -130,6 +137,12 @@ def test_exempt_from_layout_honored_by_render_template
130 137
   ensure
131 138
     ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
132 139
   end
  140
+  
  141
+  def test_layout_is_picked_from_the_controller_instances_view_path
  142
+    @controller = PrependsViewPathController.new
  143
+    get :hello
  144
+    assert_equal 'layouts/alt', @response.layout
  145
+  end
133 146
 end
134 147
 
135 148
 class RenderWithTemplateOptionController < LayoutTest
@@ -178,3 +191,4 @@ def test_symlinked_layout_is_rendered
178 191
     end
179 192
   end
180 193
 end
  194
+
0  actionpack/test/fixtures/layout_tests/alt/layouts/alt.rhtml
No changes.

0 notes on commit 8c5cc66

Please sign in to comment.
Something went wrong with that request. Please try again.