Skip to content

Commit 036d6ef

Browse files
authored
Merge pull request rubocop#840 from koic/fix_a_false_positive_for_rails_action_controller_flash_before_render
[Fix rubocop#825] Fix a false positive for `Rails/ActionControllerFlashBeforeRender`
2 parents fbc59de + a7ef129 commit 036d6ef

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#825](https://github.com/rubocop/rubocop-rails/issues/825): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when using condition before `redirect_to`. ([@koic][])

lib/rubocop/cop/rails/action_controller_flash_before_render.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class ActionControllerFlashBeforeRender < Base
3737
^(send (send nil? :flash) :[]= ...)
3838
PATTERN
3939

40-
def_node_search :redirect_to?, <<~PATTERN
41-
(send nil? :redirect_to ...)
40+
def_node_search :render?, <<~PATTERN
41+
(send nil? :render ...)
4242
PATTERN
4343

4444
def_node_search :action_controller?, <<~PATTERN
@@ -53,7 +53,7 @@ class ActionControllerFlashBeforeRender < Base
5353
def on_send(flash_node)
5454
return unless flash_assignment?(flash_node)
5555

56-
return if followed_by_redirect_to?(flash_node)
56+
return unless followed_by_render?(flash_node)
5757

5858
return unless instance_method_or_block?(flash_node)
5959

@@ -66,13 +66,15 @@ def on_send(flash_node)
6666

6767
private
6868

69-
def followed_by_redirect_to?(flash_node)
69+
def followed_by_render?(flash_node)
7070
flash_assigment_node = find_ancestor(flash_node, type: :send)
71-
context = flash_assigment_node.parent
71+
context = flash_assigment_node
72+
context = context.parent if context.parent.if_type?
73+
context = context.right_siblings
74+
return true if context.empty?
7275

73-
flash_index = context.children.index(flash_assigment_node)
74-
context.each_child_node.with_index.any? do |node, index|
75-
index > flash_index && redirect_to?(node)
76+
context.compact.any? do |node|
77+
render?(node)
7678
end
7779
end
7880

spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class NonController < ApplicationRecord
104104
context 'with a condition' do
105105
%w[ActionController::Base ApplicationController].each do |parent_class|
106106
context "within a class inherited from #{parent_class}" do
107-
it 'registers an offense and corrects' do
107+
it 'registers an offense and corrects when using `flash` before `render`' do
108108
expect_offense(<<~RUBY)
109109
class HomeController < #{parent_class}
110110
def create
@@ -124,6 +124,34 @@ def create
124124
end
125125
RUBY
126126
end
127+
128+
it 'does not register an offense when using `flash` before `redirect_to`' do
129+
expect_no_offenses(<<~RUBY)
130+
class HomeController < #{parent_class}
131+
def create
132+
if condition
133+
flash[:alert] = "msg"
134+
end
135+
136+
redirect_to :index
137+
end
138+
end
139+
RUBY
140+
end
141+
142+
it 'does not register an offense when using `flash` before `redirect_back`' do
143+
expect_no_offenses(<<~RUBY)
144+
class HomeController < #{parent_class}
145+
def create
146+
if condition
147+
flash[:alert] = "msg"
148+
end
149+
150+
redirect_back fallback_location: root_path
151+
end
152+
end
153+
RUBY
154+
end
127155
end
128156
end
129157

0 commit comments

Comments
 (0)