Skip to content

Commit 87e5bbf

Browse files
author
5hun-s
committed
[Fix #1182] Fix a false positive for Rails/ActionControllerFlashBeforeRender
1 parent ec292a2 commit 87e5bbf

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1182](https://github.com/rubocop/rubocop-rails/issues/1182): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when flash is called in a block. ([@5hun-s][])

lib/rubocop/cop/rails/action_controller_flash_before_render.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def followed_by_render?(flash_node)
7373
return false if use_redirect_to?(context)
7474

7575
context = node
76-
elsif context.right_siblings.empty?
76+
elsif context.right_siblings.empty? && !use_redirect_to?(context.parent)
7777
return true
7878
end
7979
context = context.right_siblings

spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,4 +395,36 @@ def create
395395
RUBY
396396
end
397397
end
398+
399+
context 'when using `flash` in a one-line iteration block before `redirect_to`' do
400+
it 'does not register an offense' do
401+
expect_no_offenses(<<~RUBY)
402+
class HomeController < ApplicationController
403+
def create
404+
messages = %w[foo bar baz]
405+
messages.each { |message| flash[:alert] = message }
406+
407+
redirect_to :index
408+
end
409+
end
410+
RUBY
411+
end
412+
end
413+
414+
context 'when using `flash` in a multi-line block before `redirect_to`' do
415+
it 'does not register an offense' do
416+
expect_no_offenses(<<~RUBY)
417+
class HomeController < ApplicationController
418+
def create
419+
messages = %w[foo bar baz]
420+
messages.each do |message|
421+
flash[:alert] = message
422+
end
423+
424+
redirect_to :index
425+
end
426+
end
427+
RUBY
428+
end
429+
end
398430
end

0 commit comments

Comments
 (0)