Skip to content
This repository
Browse code

Get Base2 layouts to work :)

  • Loading branch information...
commit 3cecbc21e37f772a72917f80c53a7645f81d94c5 1 parent 95c9718
Yehuda Katz authored April 07, 2009
34  actionpack/lib/action_controller/abstract/layouts.rb
@@ -17,6 +17,16 @@ def _implied_layout_name
17 17
         name.underscore
18 18
       end
19 19
       
  20
+      # Takes the specified layout and creates a _layout method to be called
  21
+      # by _default_layout
  22
+      # 
  23
+      # If the specified layout is a:
  24
+      # String:: return the string
  25
+      # Symbol:: call the method specified by the symbol
  26
+      # false::  return nil
  27
+      # none::   If a layout is found in the view paths with the controller's
  28
+      #          name, return that string. Otherwise, use the superclass'
  29
+      #          layout (which might also be implied)
20 30
       def _write_layout_method
21 31
         case @_layout
22 32
         when String
@@ -46,25 +56,23 @@ def _render_template(template, options)
46 56
   private
47 57
   
48 58
     def _layout() end # This will be overwritten
49  
-
50  
-    def _layout_for_option(name)
51  
-      case name
52  
-      when String     then _layout_for_name(name)
53  
-      when true       then _default_layout(true)
54  
-      when false, nil then nil
55  
-      else
56  
-        raise ArgumentError, 
57  
-          "String, true, or false, expected for `layout'; you passed #{name.inspect}"        
58  
-      end
59  
-    end
60 59
     
61 60
     def _layout_for_name(name)
62  
-      view_paths.find_by_parts(name, formats, "layouts")
  61
+      unless [String, FalseClass, NilClass].include?(name.class)
  62
+        raise ArgumentError, "String, false, or nil expected; you passed #{name.inspect}"
  63
+      end
  64
+      
  65
+      name && view_paths.find_by_parts(name, formats, "layouts")
63 66
     end
64 67
     
65 68
     def _default_layout(require_layout = false)
  69
+      if require_layout && !_layout
  70
+        raise ArgumentError, 
  71
+          "There was no default layout for #{self.class} in #{view_paths.inspect}"
  72
+      end
  73
+        
66 74
       begin
67  
-        _layout_for_option(_layout)
  75
+        layout = _layout_for_name(_layout)
68 76
       rescue NameError => e
69 77
         raise NoMethodError, 
70 78
           "You specified #{@_layout.inspect} as the layout, but no such method was found"
23  actionpack/lib/action_controller/new_base/layouts.rb
@@ -3,12 +3,35 @@ module Layouts
3 3
     depends_on ActionController::Renderer
4 4
     depends_on AbstractController::Layouts
5 5
     
  6
+    module ClassMethods
  7
+      def _implied_layout_name
  8
+        controller_path
  9
+      end
  10
+    end
  11
+    
6 12
     def render_to_string(options)
  13
+      # render :text => ..., :layout => ...
  14
+      # or
  15
+      # render :anything_else
7 16
       if !options.key?(:text) || options.key?(:layout)
8 17
         options[:_layout] = options.key?(:layout) ? _layout_for_option(options[:layout]) : _default_layout
9 18
       end
10 19
       
11 20
       super
12 21
     end
  22
+    
  23
+  private
  24
+  
  25
+    def _layout_for_option(name)
  26
+      case name
  27
+      when String     then _layout_for_name(name)
  28
+      when true       then _default_layout(true)
  29
+      when false, nil then nil
  30
+      else
  31
+        raise ArgumentError, 
  32
+          "String, true, or false, expected for `layout'; you passed #{name.inspect}"        
  33
+      end
  34
+    end
  35
+    
13 36
   end
14 37
 end
13  actionpack/test/abstract_controller/layouts_test.rb
@@ -144,7 +144,6 @@ def index
144 144
     AbstractController::Base.subclasses.each do |klass|
145 145
       klass = klass.constantize
146 146
       next unless klass < AbstractController::Layouts
147  
-      p klass
148 147
       klass.class_eval do
149 148
         _write_layout_method
150 149
       end
@@ -217,7 +216,17 @@ class TestBase < ActiveSupport::TestCase
217 216
         "parent has specified a layout, use the child controller layout" do
218 217
           result = WithChildOfImplied.process(:index)
219 218
           assert_equal "With Implied Hello string!", result.response_obj[:body]
220  
-      end      
  219
+      end
  220
+      
  221
+      test "raises an exception when specifying layout true" do
  222
+        assert_raises ArgumentError do
  223
+          Object.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
  224
+            class ::BadOmgFailLolLayout < AbstractControllerTests::Layouts::Base
  225
+              layout true
  226
+            end
  227
+          RUBY_EVAL
  228
+        end
  229
+      end
221 230
     end
222 231
   end
223 232
 end
12  actionpack/test/new_base/render_action_test.rb
@@ -91,7 +91,9 @@ class TestLayoutTrue < SimpleRouteCase
91 91
     describe "rendering a normal template with full path with layout => true"
92 92
     
93 93
     test "raises an exception when requesting a layout and none exist" do
94  
-      assert_raise(ActionView::MissingTemplate) { get "/render_action/basic/hello_world_with_layout" }
  94
+      assert_raise(ArgumentError, /no default layout for RenderAction::BasicController in/) do 
  95
+        get "/render_action/basic/hello_world_with_layout"
  96
+      end
95 97
     end
96 98
   end
97 99
   
@@ -124,13 +126,13 @@ class TestCustomLayout < SimpleRouteCase
124 126
 module RenderActionWithApplicationLayout
125 127
   
126 128
   # # ==== Render actions with layouts ====
127  
-  
128  
-  class BasicController < ActionController::Base2
  129
+
  130
+  class BasicController < ::ApplicationController
129 131
     # Set the view path to an application view structure with layouts
130 132
     self.view_paths = self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
131 133
       "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!",
132  
-      "layouts/application.html.erb"                                           => "OHAI <%= yield %> KTHXBAI",
133  
-      "layouts/greetings.html.erb"                                             => "Greetings <%= yield %> Bai"
  134
+      "layouts/application.html.erb"                                     => "OHAI <%= yield %> KTHXBAI",
  135
+      "layouts/greetings.html.erb"                                       => "Greetings <%= yield %> Bai"
134 136
     )]
135 137
     
136 138
     def hello_world
45  actionpack/test/new_base/render_layout_test.rb
... ...
@@ -0,0 +1,45 @@
  1
+require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")
  2
+
  3
+module ControllerLayouts
  4
+  class ImplicitController < ::ApplicationController
  5
+    
  6
+    self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
  7
+      "layouts/application.html.erb" => "OMG <%= yield %> KTHXBAI",
  8
+      "basic.html.erb" => "Hello world!"
  9
+    )]
  10
+    
  11
+    def index
  12
+      render :template => "basic"
  13
+    end
  14
+  end
  15
+  
  16
+  class TestImplicitLayout < SimpleRouteCase
  17
+    describe "rendering a normal template, but using the implicit layout"
  18
+    
  19
+    get "/controller_layouts/implicit/index"
  20
+    assert_body   "OMG Hello world! KTHXBAI"
  21
+    assert_status 200
  22
+  end
  23
+  
  24
+  class ImplicitNameController < ::ApplicationController
  25
+    
  26
+    self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
  27
+      "layouts/controller_layouts/implicit_name.html.erb" => "OMGIMPLICIT <%= yield %> KTHXBAI",
  28
+      "basic.html.erb" => "Hello world!"
  29
+    )]
  30
+        
  31
+    def index
  32
+      render :template => "basic"
  33
+    end
  34
+  end
  35
+  
  36
+  class TestImplicitNamedLayout < SimpleRouteCase
  37
+    describe "rendering a normal template, but using an implicit NAMED layout"
  38
+    
  39
+    get "/controller_layouts/implicit_name/index"
  40
+    assert_body   "OMGIMPLICIT Hello world! KTHXBAI"
  41
+    assert_status 200
  42
+  end
  43
+  
  44
+  
  45
+end
4  actionpack/test/new_base/render_template_test.rb
@@ -57,8 +57,8 @@ class TestTemplateRenderInTopDirectoryWithSlash < SimpleRouteCase
57 57
     assert_body   "Elastica"
58 58
     assert_status 200
59 59
   end
60  
-  
61  
-  class RenderTemplateWithLayoutController < ActionController::Base2
  60
+    
  61
+  class RenderTemplateWithLayoutController < ::ApplicationController
62 62
     
63 63
     self.view_paths = [ActionView::FixtureTemplate::FixturePath.new(
64 64
       "test/basic.html.erb"          => "Hello from basic.html.erb",
12  actionpack/test/new_base/test_helper.rb
@@ -69,6 +69,15 @@ class Rack::TestCase < ActiveSupport::TestCase
69 69
     end
70 70
     
71 71
     ActionController::Routing.use_controllers!(controllers)
  72
+    
  73
+    # Move into a bootloader
  74
+    AbstractController::Base.subclasses.each do |klass|
  75
+      klass = klass.constantize
  76
+      next unless klass < AbstractController::Layouts
  77
+      klass.class_eval do
  78
+        _write_layout_method
  79
+      end
  80
+    end    
72 81
   end
73 82
     
74 83
   def app
@@ -123,6 +132,9 @@ def self.assert_header(name, value)
123 132
   
124 133
 end
125 134
 
  135
+class ::ApplicationController < ActionController::Base2
  136
+end
  137
+
126 138
 class SimpleRouteCase < Rack::TestCase
127 139
   setup do
128 140
     ActionController::Routing::Routes.draw do |map|

0 notes on commit 3cecbc2

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