Skip to content

Commit

Permalink
[Issue rubocop#1269] Add tests for `Rails/ActionControllerFlashBefore…
Browse files Browse the repository at this point in the history
…Render` from issue rubocop#1269

This cop would need to do control flow analysis which it just doesn't do. RuboCop also has no mechanism for that.

So just reverting this for now to fix the newly introduces false positives
  • Loading branch information
Earlopain committed Aug 27, 2024
1 parent 2abca62 commit 4f4fff5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_revert_flash_before_render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1269](https://github.com/rubocop/rubocop-rails/issues/1269): Fix positives for `Rails/ActionControllerFlashBeforeRender` in combination with implicit returns. ([@earlopain][])
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,71 @@ def create
RUBY
end
end

context 'when using `flash` after `render` and `redirect_to` is used in implicit return branch ' \
'and render is is used in the other branch' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
def create
if foo.update(params)
flash[:success] = 'msg'
if redirect_to_index?
redirect_to index
else
redirect_to path(foo)
end
else
flash.now[:alert] = 'msg'
render :edit, status: :unprocessable_entity
end
end
end
RUBY
end
end

context 'when using `flash` after `render` and `render` is part of a different preceding branch' \
'that implicitly returns' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
def create
if remote_request? || sandbox?
if current_user.nil?
render :index
else
head :forbidden
end
elsif current_user.nil?
redirect_to sign_in_path
else
flash[:alert] = 'msg'
if request.referer.present?
redirect_to(request.referer)
else
redirect_to(root_path)
end
end
end
end
RUBY
end
end

context 'when using `flash` in `rescue` and `redirect_to` in `ensure`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class HomeController < ApplicationController
def create
rescue
flash[:alert] = 'msg'
ensure
redirect_to :index
end
end
RUBY
end
end
end

0 comments on commit 4f4fff5

Please sign in to comment.