Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive in Rails/ActionControllerFlashBeforeRender when using multiple flashes + conditions #1269

Open
jamescook opened this issue Apr 12, 2024 · 0 comments

Comments

@jamescook
Copy link


Expected behavior

This code should be valid:

        class HomeController < ApplicationController
          def update
            if condition
              flash[:danger] = "msg"
              redirect_to root_path and return
            end

            if other_condition
              flash[:success] = "other msg"
              redirect_to root_path
            else
              flash.now[:danger] = "other other msg"
              render 'something', status: :unprocessable_entity
            end
          end
        end

Actual behavior

Rubocop says to use flash.now before render, which I think is wrong:

          def update
            if condition
              flash[:danger] = "msg"
       +      ^^^^^ Use `flash.now` before `render`.
              redirect_to root_path and return
            end

Steps to reproduce the problem

--- a/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb
+++ b/spec/rubocop/cop/rails/action_controller_flash_before_render_spec.rb
@@ -328,4 +328,27 @@ RSpec.describe RuboCop::Cop::Rails::ActionControllerFlashBeforeRender, :config d
       RUBY
     end
   end
+
+  context 'when using `flash` before `render` and returning `redirect_to` in condition block - part deux' do
+    it 'does not register an offense' do
+      expect_no_offenses(<<~RUBY)
+        class HomeController < ApplicationController
+          def update
+            if condition
+              flash[:danger] = "msg" # breaks here
+              redirect_to root_path and return
+            end
+
+            if other_condition
+              flash[:success] = "other msg"
+              redirect_to root_path
+            else
+              flash.now[:danger] = "other other msg"
+              render 'something', status: :unprocessable_entity
+            end
+          end
+        end
+      RUBY
+    end
+  end

Spec passes if I change the example code to use return redirect_to root_path inside the offending condition so I suspect it's related to the usage of redirect_to ... and return. However, the spec will pass when using redirect_to root_path and return if I remove the code starting with if other_condition through the end of the Rails action.

RuboCop version

bundle exec rubocop -V
1.63.1 (using Parser 3.3.0.5, rubocop-ast 1.31.2, running on ruby 3.1.4) [arm64-darwin22]

  • rubocop-performance 1.21.0
  • rubocop-rails 2.24.1
  • rubocop-rspec 2.27.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant