aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/new_base/render_action_test.rb
blob: d5896c1ebd2d78ee2dababc6e6b49bf43996cde5 (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
require 'abstract_unit'

module RenderAction
  # This has no layout and it works
  class BasicController < ActionController::Base
    self.view_paths = [ActionView::FixtureResolver.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 RenderActionTest < SimpleRouteCase
    test "rendering an action using :action => <String>" do
      get "/render_action/basic/hello_world"

      assert_body "Hello world!"
      assert_status 200
    end

    test "rendering an action using '<action>'" do
      get "/render_action/basic/hello_world_as_string"

      assert_body "Hello world!"
      assert_status 200
    end

    test "rendering an action using '<action>' and options" do
      get "/render_action/basic/hello_world_as_string_with_options"

      assert_body "Hello world!"
      assert_status 404
    end

    test "rendering an action using :action" do
      get "/render_action/basic/hello_world_as_symbol"

      assert_body "Hello world!"
      assert_status 200
    end

    test "rendering an action using :action => :hello_world" do
      get "/render_action/basic/hello_world_with_symbol"

      assert_body "Hello world!"
      assert_status 200
    end
  end

  class RenderLayoutTest < SimpleRouteCase
    describe "Both <controller_path>.html.erb and application.html.erb are missing"

    test "rendering with layout => true" do
      assert_raise(ArgumentError, /no default layout for RenderAction::BasicController in/) do
        get "/render_action/basic/hello_world_with_layout", {}, "action_dispatch.show_exceptions" => false
      end
    end

    test "rendering with layout => false" do
      get "/render_action/basic/hello_world_with_layout_false"

      assert_body "Hello world!"
      assert_status 200
    end

    test "rendering with layout => :nil" do
      get "/render_action/basic/hello_world_with_layout_nil"

      assert_body "Hello world!"
      assert_status 200
    end

    test "rendering with layout => 'greetings'" do
      assert_raise(ActionView::MissingTemplate) do
        get "/render_action/basic/hello_world_with_custom_layout", {}, "action_dispatch.show_exceptions" => false
      end
    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::FixtureResolver.new(
      "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!",
      "render_action_with_application_layout/basic/hello.html.builder"   => "xml.p 'Omg'",
      "layouts/application.html.erb"                                     => "OHAI <%= yield %> KTHXBAI",
      "layouts/greetings.html.erb"                                       => "Greetings <%= yield %> Bai",
      "layouts/builder.html.builder"                                     => "xml.html do\n  xml << yield\nend"
    )]

    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

    def with_builder_and_layout
      render :action => "hello", :layout => "builder"
    end
  end

  class LayoutTest < SimpleRouteCase
    describe "Only application.html.erb is present and <controller_path>.html.erb is missing"

    test "rendering implicit application.html.erb as layout" do
      get "/render_action_with_application_layout/basic/hello_world"

      assert_body "OHAI Hello World! KTHXBAI"
      assert_status 200
    end

    test "rendering with layout => true" do
      get "/render_action_with_application_layout/basic/hello_world_with_layout"

      assert_body "OHAI Hello World! KTHXBAI"
      assert_status 200
    end

    test "rendering with layout => false" do
      get "/render_action_with_application_layout/basic/hello_world_with_layout_false"

      assert_body "Hello World!"
      assert_status 200
    end

    test "rendering with layout => :nil" do
      get "/render_action_with_application_layout/basic/hello_world_with_layout_nil"

      assert_body "Hello World!"
      assert_status 200
    end

    test "rendering with layout => 'greetings'" do
      get "/render_action_with_application_layout/basic/hello_world_with_custom_layout"

      assert_body "Greetings Hello World! Bai"
      assert_status 200
    end
  end

  class TestLayout < SimpleRouteCase
    testing BasicController

    test "builder works with layouts" do
      get :with_builder_and_layout
      assert_response "<html>\n<p>Omg</p>\n</html>\n"
    end
  end

end

module RenderActionWithControllerLayout
  class BasicController < ActionController::Base
    self.view_paths = self.view_paths = [ActionView::FixtureResolver.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 ControllerLayoutTest < SimpleRouteCase
    describe "Only <controller_path>.html.erb is present and application.html.erb is missing"

    test "render hello_world and implicitly use <controller_path>.html.erb as a layout." do
      get "/render_action_with_controller_layout/basic/hello_world"

      assert_body "With Controller Layout! Hello World! KTHXBAI"
      assert_status 200
    end

    test "rendering with layout => true" do
      get "/render_action_with_controller_layout/basic/hello_world_with_layout"

      assert_body "With Controller Layout! Hello World! KTHXBAI"
      assert_status 200
    end

    test "rendering with layout => false" do
      get "/render_action_with_controller_layout/basic/hello_world_with_layout_false"

      assert_body "Hello World!"
      assert_status 200
    end

    test "rendering with layout => :nil" do
      get "/render_action_with_controller_layout/basic/hello_world_with_layout_nil"

      assert_body "Hello World!"
      assert_status 200
    end
  end
end

module RenderActionWithBothLayouts
  class BasicController < ActionController::Base
    self.view_paths = [ActionView::FixtureResolver.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 ControllerLayoutTest < SimpleRouteCase
    describe "Both <controller_path>.html.erb and application.html.erb are present"

    test "rendering implicitly use <controller_path>.html.erb over application.html.erb as a layout" do
      get "/render_action_with_both_layouts/basic/hello_world"

      assert_body   "With Controller Layout! Hello World! KTHXBAI"
      assert_status 200
    end

    test "rendering with layout => true" do
      get "/render_action_with_both_layouts/basic/hello_world_with_layout"

      assert_body "With Controller Layout! Hello World! KTHXBAI"
      assert_status 200
    end

    test "rendering with layout => false" do
      get "/render_action_with_both_layouts/basic/hello_world_with_layout_false"

      assert_body "Hello World!"
      assert_status 200
    end

    test "rendering with layout => :nil" do
      get "/render_action_with_both_layouts/basic/hello_world_with_layout_nil"

      assert_body "Hello World!"
      assert_status 200
    end
  end
end