/
render_spec.rb
369 lines (297 loc) · 12.3 KB
/
render_spec.rb
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
require File.expand_path("#{File.dirname(__FILE__)}/rails_spec_helper")
describe ActionController::Base do
class TestController < ActionController::Base
# Let exceptions propagate rather than generating the usual error page.
include ActionController::TestCase::RaiseActionExceptions
# We need this, because we reference Views::Test::Needs below, and it
# doesn't auto-load otherwise.
require 'views/test/needs.html.rb'
# replicate deprecated use for rails <3.2
if (Gem::Version.new(Rails.version) < Gem::Version.new('3.2.0') rescue false)
def render(*args, &block)
options = args.extract_options!
if options[:template]
handlers = options.delete(:handlers)
format = '.html' unless options.delete(:bare)
options[:template] += "#{format}.#{handlers.first}"
end
render(*(args << options), &block)
end
end
def render_widget_class
@foobar = "foobar"
render :widget => TestWidget
end
def render_widget_with_ignored_controller_variables
@foo = "foo"
@baz = "baz"
render :widget => NeedsWidget
end
def render_widget_with_extra_controller_variables
with_ignoring_extra_controller_assigns(NeedsWidget, false) do
@foo = "foo"
@baz = "baz"
render :widget => NeedsWidget
end
end
def render_widget_instance
render :widget => TestWidget.new(:foobar => "foobar")
end
def render_with_content_method
render :widget => TestWidget, :content_method_name => :content_method
end
def render_with_rails_options
render :widget => TestWidget, :status => 500, :content_type => "application/json"
end
def render_template_with_implicit_assigns
@foobar = "foobar"
render :template => "test/implicit_assigns", :handlers => [:rb]
end
def render_template_with_protected_instance_variable
render :template => "test/protected_instance_variable", :handlers => [:rb]
end
def render_template_with_excess_variables
with_ignoring_extra_controller_assigns(Views::Test::Needs, false) do
@foobar = "foobar"
@barfoo = "barfoo"
render :template => 'test/render_default', :handlers => [:rb]
end
end
def render_needs_template_with_excess_variables
with_ignoring_extra_controller_assigns(Views::Test::Needs, false) do
@foobar = "foobar"
@barfoo = "barfoo"
render :template => 'test/needs', :handlers => [:rb]
end
end
def with_ignoring_extra_controller_assigns(klass, value)
old_value = klass.ignore_extra_controller_assigns
begin
klass.ignore_extra_controller_assigns = value
yield
ensure
klass.ignore_extra_controller_assigns = old_value
end
end
def render_needs_template_with_excess_variables_and_ignoring_extras
@foobar = "foobar"
@barfoo = "barfoo"
render :template => 'test/needs', :handlers => [:rb]
end
def render_needs_subclass_template_with_excess_variables_and_ignoring_extras
@foobar = "foobar"
@barfoo = "barfoo"
render :template => 'test/needs_subclass', :handlers => [:rb]
end
def render_bare_rb
render :template => "test/bare", :handlers => [:rb], :bare => true
end
def render_default
@foobar = "foobar"
end
def render_template_with_partial
@foobar = "foobar"
render :template => "test/render_partial", :handlers => [:rb]
end
def render_erb_from_erector
@foobar = "foobar"
render :template => "test/erb_from_erector", :handlers => [:rb]
end
def render_erector_from_erb
@foobar = "foobar"
render :template => "test/erector_from_erb", :handlers => [:erb]
end
def render_erector_with_locals_from_erb
@local_foo = "hihi"
@local_bar = "byebye"
render :template => 'test/erector_with_locals_from_erb', :handlers => [:erb]
end
def render_erector_with_locals_from_erb_defaulted
@local_foo = "hihi"
render :template => 'test/erector_with_locals_from_erb', :handlers => [:erb]
end
def render_erector_with_locals_from_erb_override
@foo = "globalfoo"
@local_foo = "localfoo"
render :template => 'test/erector_with_locals_from_erb', :handlers => [:erb]
end
def render_erector_with_locals_from_erb_not_needed
@local_foo = "localfoo"
@local_baz = "unneeded"
render :template => 'test/erector_with_locals_from_erb', :handlers => [:erb]
end
def render_erector_partial_with_unneeded_controller_variables
@local_foo = "localfoo"
@baz = "unneeded"
render :template => 'test/erector_with_locals_from_erb', :handlers => [:erb]
end
def with_controller_assigns_propagate_to_partials(klass, value)
old_value = klass.controller_assigns_propagate_to_partials
begin
klass.controller_assigns_propagate_to_partials = value
yield
ensure
klass.controller_assigns_propagate_to_partials = old_value
end
end
def render_erector_partial_without_controller_variables
with_controller_assigns_propagate_to_partials(Views::Test::PartialWithLocals, false) do
@local_foo = "localfoo"
@bar = "barbar"
render :template => 'test/erector_with_locals_from_erb', :handlers => [:erb]
end
end
def render_with_needs
@foo = "foo"
@bar = "bar"
render :widget => NeedsWidget
end
def render_with_widget_as_layout
render :layout => "layouts/widget_as_layout"
end
def render_default_widget_with_layout
@widget_content = "widget content"
@layout_content = "layout content"
end
def render_default_erb_with_layout
@erb_content = "erb content"
@layout_content = "layout content"
end
def render_with_widget_as_layout_and_vars
@before = "Breakfast"
@during = "Lunch"
@after = "Dinner"
render :template => "test/render_with_widget_as_layout", :layout => "layouts/widget_as_layout"
end
def render_with_widget_as_layout_using_content_for
render :template => "test/render_with_widget_as_layout_using_content_for", :layout => "layouts/widget_as_layout"
end
end
class TestWidget < Erector::Widget
def content
text @foobar
end
def content_method
text "content_method"
end
end
class TestFormWidget < Erector::Widget
def content
form_tag('/') do
h1 "Create a foo"
rawtext text_field_tag(:name)
end
end
end
class NeedsWidget < Erector::Widget
needs :foo, :bar => true
def content
text "foo #{@foo} bar #{@bar}"
end
end
def test_action(action)
@response = TestController.action(action).call(Rack::MockRequest.env_for("/path"))[2]
@response.body
end
describe "#render" do
it "should render a widget class with implicit assigns" do
test_action(:render_widget_class).should == "foobar"
end
it "should render a widget instance with explicit assigns" do
test_action(:render_widget_instance).should == "foobar"
end
it "should render a widget class with implicit assigns and ignoring extra variables" do
test_action(:render_widget_with_ignored_controller_variables).should == "foo foo bar true"
end
it "should raise when rendering a widget class with implicit assigns and too many variables" do
proc { test_action(:render_widget_with_extra_controller_variables) }.should raise_error(ArgumentError, /Excess parameters?.*: .*baz/)
end
it "should render a specific content method" do
test_action(:render_with_content_method).should == "content_method"
end
it "should pass rails options to base render method" do
test_action(:render_with_rails_options)
@response.response_code.should == 500
@response.content_type.should == "application/json"
end
it "should render a template with implicit assigns" do
test_action(:render_template_with_implicit_assigns).should == "foobar"
end
it "should not include protected instance variables in assigns" do
test_action(:render_template_with_protected_instance_variable).should == ""
end
it "should render a template without a .html format included" do
test_action(:render_bare_rb).should == "Bare"
end
it "should render a template with excess controller variables" do
test_action(:render_template_with_excess_variables).should == "Default foobar"
end
it "should raise if rendering a #needs template with excess controller variables" do
proc { test_action(:render_needs_template_with_excess_variables) }.should raise_error(ActionView::TemplateError, /Excess parameters?.*: .*barfoo/)
end
it "should render a #needs template with excess controller variables and ignore_extra_controller_assigns" do
test_action(:render_needs_template_with_excess_variables_and_ignoring_extras).should == "Needs foobar"
end
it "should respect ignore_extra_controller_assigns in subclasses" do
test_action(:render_needs_subclass_template_with_excess_variables_and_ignoring_extras).should == "NeedsSubclass foobar"
end
it "should render a template which uses partials" do
test_action(:render_template_with_partial).should == "Partial foobar"
end
it "should render an erector widget which uses an ERB partial'" do
test_action(:render_erb_from_erector).should == "Partial foobar"
end
it "should render an ERB template which uses an erector widget partial" do
test_action(:render_erector_from_erb).should == "Partial foobar"
end
it "should render an ERB template which uses an erector widget partial with locals" do
test_action(:render_erector_with_locals_from_erb).should == "Partial, foo hihi, bar byebye"
end
it "should render an ERB template which uses an erector widget partial with a defaulted local" do
test_action(:render_erector_with_locals_from_erb_defaulted).should == "Partial, foo hihi, bar 12345"
end
it "should override instance variables with local variables when rendering partials" do
test_action(:render_erector_with_locals_from_erb_override).should == "Partial, foo localfoo, bar 12345"
end
it "should raise if passing a local that's not needed" do
proc { test_action(:render_erector_with_locals_from_erb_not_needed) }.should raise_error(ActionView::TemplateError, /Excess parameters?.*: .*baz/)
end
it "should not pass unneeded controller variables to a partial" do
test_action(:render_erector_partial_with_unneeded_controller_variables).should == "Partial, foo localfoo, bar 12345"
end
it "should not pass controller variables to a partial at all, if requested" do
test_action(:render_erector_partial_without_controller_variables).should == "Partial, foo localfoo, bar 12345"
end
it "should render a default template" do
test_action(:render_default).should == "Default foobar"
end
it "should render a default erb template with default erb layout" do
TestController.layout 'erb_as_layout'
test_action(:render_default_erb_with_layout).should == "layout content\nerb content"
end
it "should render a default widget with default erb layout" do
TestController.layout 'erb_as_layout'
test_action(:render_default_widget_with_layout).should == "layout content\nwidget content"
end
it "should render a default erb template with default widget layout" do
TestController.layout 'widget_as_layout'
test_action(:render_default_erb_with_layout).should == "BEFOREerb contentAFTER"
end
it "should render a default widget with default widget layout" do
TestController.layout 'widget_as_layout'
test_action(:render_default_widget_with_layout).should == "BEFOREwidget contentAFTER"
end
it "should allow rendering widget with needs" do
proc { test_action(:render_with_needs) }.should_not raise_error
end
it "should allow using a widget as a layout" do
test_action(:render_with_widget_as_layout).should == "BEFOREDURINGAFTER"
end
it "should allow using a widget as a layout with instance vars" do
test_action(:render_with_widget_as_layout_and_vars).should == "BreakfastLunchDinner"
end
it "should allow using a widget as a layout using content_for" do
test_action(:render_with_widget_as_layout_using_content_for).should == "TOPBEFOREDURINGAFTER"
end
end
end