aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/new_base/render_action_test.rb
blob: 348d70381ba1ce8e7ab24d8d10fbe787573150f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper")

module RenderAction
  
  # This has no layout and it works
  class BasicController < ActionController::Base
    
    self.view_paths = [ActionView::Template::FixturePath.new(
      "render_action/basic/hello_world.html.erb" => "Hello world!"
    )]
    
    def hello_world
      render :action => "hello_world"
    end
    
    def hello_world_as_string
      render "hello_world"
    end
    
    def hello_world_as_string_with_options
      render "hello_world", :status => 404
    end
    
    def hello_world_as_symbol
      render :hello_world
    end

    def hello_world_with_symbol
      render :action => :hello_world
    end
    
    def hello_world_with_layout
      render :action => "hello_world", :layout => true
    end
    
    def hello_world_with_layout_false
      render :action => "hello_world", :layout => false
    end
    
    def hello_world_with_layout_nil
      render :action => "hello_world", :layout => nil
    end
    
    def hello_world_with_custom_layout
      render :action => "hello_world", :layout => "greetings"
    end
    
  end
  
  class TestBasic < SimpleRouteCase
    describe "Rendering an action using :action => <String>"
    
    get "/render_action/basic/hello_world"
    assert_body   "Hello world!"
    assert_status 200
  end
  
  class TestWithString < SimpleRouteCase
    describe "Render an action using 'hello_world'"
    
    get "/render_action/basic/hello_world_as_string"
    assert_body   "Hello world!"
    assert_status 200
  end
  
  class TestWithStringAndOptions < SimpleRouteCase
    describe "Render an action using 'hello_world'"
    
    get "/render_action/basic/hello_world_as_string_with_options"
    assert_body   "Hello world!"
    assert_status 404
  end
  
  class TestAsSymbol < SimpleRouteCase
    describe "Render an action using :hello_world"
    
    get "/render_action/basic/hello_world_as_symbol"
    assert_body   "Hello world!"
    assert_status 200
  end
  
  class TestWithSymbol < SimpleRouteCase
    describe "Render an action using :action => :hello_world"
    
    get "/render_action/basic/hello_world_with_symbol"
    assert_body   "Hello world!"
    assert_status 200
  end
  
  class TestLayoutTrue < SimpleRouteCase
    describe "rendering a normal template with full path with layout => true"
    
    test "raises an exception when requesting a layout and none exist" do
      assert_raise(ArgumentError, /no default layout for RenderAction::BasicController in/) do 
        get "/render_action/basic/hello_world_with_layout"
      end
    end
  end
  
  class TestLayoutFalse < SimpleRouteCase
    describe "rendering a normal template with full path with layout => false"
    
    get "/render_action/basic/hello_world_with_layout_false"
    assert_body   "Hello world!"
    assert_status 200
  end
  
  class TestLayoutNil < SimpleRouteCase
    describe "rendering a normal template with full path with layout => :nil"
    
    get "/render_action/basic/hello_world_with_layout_nil"
    assert_body   "Hello world!"
    assert_status 200
  end
  
  class TestCustomLayout < SimpleRouteCase
    describe "rendering a normal template with full path with layout => 'greetings'"
    
    test "raises an exception when requesting a layout that does not exist" do
      assert_raise(ActionView::MissingTemplate) { get "/render_action/basic/hello_world_with_custom_layout" }
    end
  end
  
end

module RenderActionWithApplicationLayout
  
  # # ==== Render actions with layouts ====

  class BasicController < ::ApplicationController
    # Set the view path to an application view structure with layouts
    self.view_paths = self.view_paths = [ActionView::Template::FixturePath.new(
      "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!",
      "layouts/application.html.erb"                                     => "OHAI <%= yield %> KTHXBAI",
      "layouts/greetings.html.erb"                                       => "Greetings <%= yield %> Bai"
    )]
    
    def hello_world
      render :action => "hello_world"
    end
    
    def hello_world_with_layout
      render :action => "hello_world", :layout => true
    end
    
    def hello_world_with_layout_false
      render :action => "hello_world", :layout => false
    end
    
    def hello_world_with_layout_nil
      render :action => "hello_world", :layout => nil
    end
    
    def hello_world_with_custom_layout
      render :action => "hello_world", :layout => "greetings"
    end
  end
  
  class TestDefaultLayout < SimpleRouteCase
    describe %(
      Render hello_world and implicitly use application.html.erb as a layout if 
      no layout is specified and no controller layout is present
    )
      
    get "/render_action_with_application_layout/basic/hello_world"
    assert_body   "OHAI Hello World! KTHXBAI"
    assert_status 200
  end
  
  class TestLayoutTrue < SimpleRouteCase
    describe "rendering a normal template with full path with layout => true"
    
    get "/render_action_with_application_layout/basic/hello_world_with_layout"
    assert_body   "OHAI Hello World! KTHXBAI"
    assert_status 200
  end
  
  class TestLayoutFalse < SimpleRouteCase
    describe "rendering a normal template with full path with layout => false"
    
    get "/render_action_with_application_layout/basic/hello_world_with_layout_false"
    assert_body   "Hello World!"
    assert_status 200
  end
  
  class TestLayoutNil < SimpleRouteCase
    describe "rendering a normal template with full path with layout => :nil"
    
    get "/render_action_with_application_layout/basic/hello_world_with_layout_nil"
    assert_body   "Hello World!"
    assert_status 200
  end
  
  class TestCustomLayout < SimpleRouteCase
    describe "rendering a normal template with full path with layout => 'greetings'"
    
    get "/render_action_with_application_layout/basic/hello_world_with_custom_layout"
    assert_body   "Greetings Hello World! Bai"
    assert_status 200
  end
  
end

module RenderActionWithControllerLayout
  
  class BasicController < ActionController::Base
    self.view_paths = self.view_paths = [ActionView::Template::FixturePath.new(
      "render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!",
      "layouts/render_action_with_controller_layout/basic.html.erb"     => "With Controller Layout! <%= yield %> KTHXBAI"
    )]
    
    def hello_world
      render :action => "hello_world"
    end
    
    def hello_world_with_layout
      render :action => "hello_world", :layout => true
    end
    
    def hello_world_with_layout_false
      render :action => "hello_world", :layout => false
    end
    
    def hello_world_with_layout_nil
      render :action => "hello_world", :layout => nil
    end
    
    def hello_world_with_custom_layout
      render :action => "hello_world", :layout => "greetings"
    end
  end
  
  class TestControllerLayout < SimpleRouteCase
    describe "Render hello_world and implicitly use <controller_path>.html.erb as a layout."

    get "/render_action_with_controller_layout/basic/hello_world"
    assert_body   "With Controller Layout! Hello World! KTHXBAI"
    assert_status 200
  end
  
  class TestLayoutTrue < SimpleRouteCase
    describe "rendering a normal template with full path with layout => true"
    
    get "/render_action_with_controller_layout/basic/hello_world_with_layout"
    assert_body   "With Controller Layout! Hello World! KTHXBAI"
    assert_status 200
  end
  
  class TestLayoutFalse < SimpleRouteCase
    describe "rendering a normal template with full path with layout => false"
    
    get "/render_action_with_controller_layout/basic/hello_world_with_layout_false"
    assert_body   "Hello World!"
    assert_status 200
  end
  
  class TestLayoutNil < SimpleRouteCase
    describe "rendering a normal template with full path with layout => :nil"
    
    get "/render_action_with_controller_layout/basic/hello_world_with_layout_nil"
    assert_body   "Hello World!"
    assert_status 200
  end
  
end

module RenderActionWithBothLayouts
  
  class BasicController < ActionController::Base
    self.view_paths = [ActionView::Template::FixturePath.new({
      "render_action_with_both_layouts/basic/hello_world.html.erb" => "Hello World!",
      "layouts/application.html.erb"                                => "OHAI <%= yield %> KTHXBAI",
      "layouts/render_action_with_both_layouts/basic.html.erb"      => "With Controller Layout! <%= yield %> KTHXBAI"
    })]
    
    def hello_world
      render :action => "hello_world"
    end
    
    def hello_world_with_layout
      render :action => "hello_world", :layout => true
    end
    
    def hello_world_with_layout_false
      render :action => "hello_world", :layout => false
    end
    
    def hello_world_with_layout_nil
      render :action => "hello_world", :layout => nil
    end
  end
  
  class TestControllerLayoutFirst < SimpleRouteCase
    describe "Render hello_world and implicitly use <controller_path>.html.erb over application.html.erb as a layout"

    get "/render_action_with_both_layouts/basic/hello_world"
    assert_body   "With Controller Layout! Hello World! KTHXBAI"
    assert_status 200
  end
  
  class TestLayoutTrue < SimpleRouteCase
    describe "rendering a normal template with full path with layout => true"
    
    get "/render_action_with_both_layouts/basic/hello_world_with_layout"
    assert_body   "With Controller Layout! Hello World! KTHXBAI"
    assert_status 200
  end
  
  class TestLayoutFalse < SimpleRouteCase
    describe "rendering a normal template with full path with layout => false"
    
    get "/render_action_with_both_layouts/basic/hello_world_with_layout_false"
    assert_body   "Hello World!"
    assert_status 200
  end
  
  class TestLayoutNil < SimpleRouteCase
    describe "rendering a normal template with full path with layout => :nil"
    
    get "/render_action_with_both_layouts/basic/hello_world_with_layout_nil"
    assert_body   "Hello World!"
    assert_status 200
  end
  
end