Skip to content

Commit

Permalink
Merge pull request #840 from koic/fix_a_false_positive_for_rails_acti…
Browse files Browse the repository at this point in the history
…on_controller_flash_before_render

[Fix #825] Fix a false positive for `Rails/ActionControllerFlashBeforeRender`
  • Loading branch information
koic authored Oct 26, 2022
2 parents fbc59de + a7ef129 commit 036d6ef
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#825](https://github.com/rubocop/rubocop-rails/issues/825): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when using condition before `redirect_to`. ([@koic][])
18 changes: 10 additions & 8 deletions lib/rubocop/cop/rails/action_controller_flash_before_render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class ActionControllerFlashBeforeRender < Base
^(send (send nil? :flash) :[]= ...)
PATTERN

def_node_search :redirect_to?, <<~PATTERN
(send nil? :redirect_to ...)
def_node_search :render?, <<~PATTERN
(send nil? :render ...)
PATTERN

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

return if followed_by_redirect_to?(flash_node)
return unless followed_by_render?(flash_node)

return unless instance_method_or_block?(flash_node)

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

private

def followed_by_redirect_to?(flash_node)
def followed_by_render?(flash_node)
flash_assigment_node = find_ancestor(flash_node, type: :send)
context = flash_assigment_node.parent
context = flash_assigment_node
context = context.parent if context.parent.if_type?
context = context.right_siblings
return true if context.empty?

flash_index = context.children.index(flash_assigment_node)
context.each_child_node.with_index.any? do |node, index|
index > flash_index && redirect_to?(node)
context.compact.any? do |node|
render?(node)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class NonController < ApplicationRecord
context 'with a condition' do
%w[ActionController::Base ApplicationController].each do |parent_class|
context "within a class inherited from #{parent_class}" do
it 'registers an offense and corrects' do
it 'registers an offense and corrects when using `flash` before `render`' do
expect_offense(<<~RUBY)
class HomeController < #{parent_class}
def create
Expand All @@ -124,6 +124,34 @@ def create
end
RUBY
end

it 'does not register an offense when using `flash` before `redirect_to`' do
expect_no_offenses(<<~RUBY)
class HomeController < #{parent_class}
def create
if condition
flash[:alert] = "msg"
end
redirect_to :index
end
end
RUBY
end

it 'does not register an offense when using `flash` before `redirect_back`' do
expect_no_offenses(<<~RUBY)
class HomeController < #{parent_class}
def create
if condition
flash[:alert] = "msg"
end
redirect_back fallback_location: root_path
end
end
RUBY
end
end
end

Expand Down

0 comments on commit 036d6ef

Please sign in to comment.