@@ -128,9 +128,36 @@ def create
128
128
end
129
129
end
130
130
RUBY
131
+
132
+ expect_offense ( <<~RUBY )
133
+ class HomeController < #{ parent_class }
134
+ def create
135
+ flash[:alert] = "msg" if condition
136
+ ^^^^^ Use `flash.now` before `render`.
137
+ end
138
+ end
139
+ RUBY
140
+
141
+ expect_correction ( <<~RUBY )
142
+ class HomeController < #{ parent_class }
143
+ def create
144
+ flash.now[:alert] = "msg" if condition
145
+ end
146
+ end
147
+ RUBY
131
148
end
132
149
133
150
it 'does not register an offense when using `flash` before `redirect_to`' do
151
+ expect_no_offenses ( <<~RUBY )
152
+ class HomeController < #{ parent_class }
153
+ def create
154
+ flash[:alert] = "msg" if condition
155
+
156
+ redirect_to :index
157
+ end
158
+ end
159
+ RUBY
160
+
134
161
expect_no_offenses ( <<~RUBY )
135
162
class HomeController < #{ parent_class }
136
163
def create
@@ -145,6 +172,16 @@ def create
145
172
end
146
173
147
174
it 'does not register an offense when using `flash` before `redirect_back`' do
175
+ expect_no_offenses ( <<~RUBY )
176
+ class HomeController < #{ parent_class }
177
+ def create
178
+ flash[:alert] = "msg" if condition
179
+
180
+ redirect_back fallback_location: root_path
181
+ end
182
+ end
183
+ RUBY
184
+
148
185
expect_no_offenses ( <<~RUBY )
149
186
class HomeController < #{ parent_class }
150
187
def create
@@ -158,7 +195,7 @@ def create
158
195
RUBY
159
196
end
160
197
161
- it 'registers an offense when using `flash` in multiline `if` branch before `render_to `' do
198
+ it 'registers an offense when using `flash` in multiline `if` branch before `render `' do
162
199
expect_offense ( <<~RUBY )
163
200
class HomeController < #{ parent_class }
164
201
def create
@@ -185,6 +222,29 @@ def create
185
222
end
186
223
end
187
224
RUBY
225
+
226
+ expect_offense ( <<~RUBY )
227
+ class HomeController < #{ parent_class }
228
+ def create
229
+ if condition
230
+ do_something
231
+ flash[:alert] = "msg"
232
+ ^^^^^ Use `flash.now` before `render`.
233
+ end
234
+ end
235
+ end
236
+ RUBY
237
+
238
+ expect_correction ( <<~RUBY )
239
+ class HomeController < #{ parent_class }
240
+ def create
241
+ if condition
242
+ do_something
243
+ flash.now[:alert] = "msg"
244
+ end
245
+ end
246
+ end
247
+ RUBY
188
248
end
189
249
190
250
it 'does not register an offense when using `flash` in multiline `if` branch before `redirect_to`' do
@@ -217,6 +277,81 @@ def create
217
277
end
218
278
end
219
279
RUBY
280
+
281
+ expect_no_offenses ( <<~RUBY )
282
+ class HomeController < #{ parent_class }
283
+ def create
284
+ if condition
285
+ flash[:alert] = "msg"
286
+ redirect_to :index
287
+
288
+ return
289
+ end
290
+ end
291
+ end
292
+ RUBY
293
+ end
294
+
295
+ it 'registers an offense when using `flash` in multiline `rescue` branch before `render`' do
296
+ expect_offense ( <<~RUBY )
297
+ class HomeController < #{ parent_class }
298
+ def create
299
+ begin
300
+ do_something
301
+ flash[:alert] = "msg in begin"
302
+ ^^^^^ Use `flash.now` before `render`.
303
+ rescue
304
+ flash[:alert] = "msg in rescue"
305
+ ^^^^^ Use `flash.now` before `render`.
306
+ end
307
+
308
+ render :index
309
+ end
310
+ end
311
+ RUBY
312
+
313
+ expect_correction ( <<~RUBY )
314
+ class HomeController < #{ parent_class }
315
+ def create
316
+ begin
317
+ do_something
318
+ flash.now[:alert] = "msg in begin"
319
+ rescue
320
+ flash.now[:alert] = "msg in rescue"
321
+ end
322
+
323
+ render :index
324
+ end
325
+ end
326
+ RUBY
327
+
328
+ expect_offense ( <<~RUBY )
329
+ class HomeController < #{ parent_class }
330
+ def create
331
+ begin
332
+ do_something
333
+ flash[:alert] = "msg in begin"
334
+ ^^^^^ Use `flash.now` before `render`.
335
+ rescue
336
+ flash[:alert] = "msg in rescue"
337
+ ^^^^^ Use `flash.now` before `render`.
338
+ end
339
+ end
340
+ end
341
+ RUBY
342
+
343
+ expect_correction ( <<~RUBY )
344
+ class HomeController < #{ parent_class }
345
+ def create
346
+ begin
347
+ do_something
348
+ flash.now[:alert] = "msg in begin"
349
+ rescue
350
+ flash.now[:alert] = "msg in rescue"
351
+ end
352
+ end
353
+ end
354
+ RUBY
220
355
end
221
356
222
357
it 'does not register an offense when using `flash` in multiline `rescue` branch before `redirect_to`' do
@@ -235,6 +370,48 @@ def create
235
370
end
236
371
RUBY
237
372
end
373
+
374
+ it 'does not register an offense when using `flash` before `redirect_to` in `rescue` branch' do
375
+ expect_no_offenses ( <<~RUBY )
376
+ class HomeController < #{ parent_class }
377
+ def create
378
+ begin
379
+ do_something
380
+ flash[:alert] = "msg in begin"
381
+ redirect_to :index
382
+
383
+ return
384
+ rescue
385
+ flash[:alert] = "msg in rescue"
386
+ redirect_to :index
387
+
388
+ return
389
+ end
390
+
391
+ render :index
392
+ end
393
+ end
394
+ RUBY
395
+
396
+ expect_no_offenses ( <<~RUBY )
397
+ class HomeController < #{ parent_class }
398
+ def create
399
+ begin
400
+ do_something
401
+ flash[:alert] = "msg in begin"
402
+ redirect_to :index
403
+
404
+ return
405
+ rescue
406
+ flash[:alert] = "msg in rescue"
407
+ redirect_to :index
408
+
409
+ return
410
+ end
411
+ end
412
+ end
413
+ RUBY
414
+ end
238
415
end
239
416
end
240
417
0 commit comments