Skip to content

Commit

Permalink
[Fix rubocop#1244] Fix a false positive for `Rails/ActionControllerFl…
Browse files Browse the repository at this point in the history
…ashBeforeRender` when returning `redirect_to`
  • Loading branch information
Earlopain committed Mar 4, 2024
1 parent 87d94e9 commit af82326
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1244](https://github.com/rubocop/rubocop-rails/issues/1244): Fix a false positive for `Rails/ActionControllerFlashBeforeRender` when returning `redirect_to`. ([@earlopain][])
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def instance_method_or_block?(node)

def use_redirect_to?(context)
context.right_siblings.compact.any? do |sibling|
# Unwrap `return redirect_to :index`
sibling = sibling.children.first if sibling.return_type? && sibling.children.one?
sibling.send_type? && sibling.method?(:redirect_to)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,20 @@ def create
RUBY
end
end

context 'when using `flash` after `render` and returning `redirect_to` in condition block' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
def create
if condition
flash[:alert] = "msg"
return redirect_to "https://www.example.com/"
end
render :index
end
end
RUBY
end
end
end

0 comments on commit af82326

Please sign in to comment.