Rubocop: False positive Rails/LexicallyScopedActionFilter when conditional statement used

Created on 12 Jan 2018  路  2Comments  路  Source: rubocop-hq/rubocop

Rails/LexicallyScopedActionFilter gives false positive when before action used with conditional statement

Expected behavior

No offenses.

Actual behavior


app/controllers/api/test.rb:4:3: C: Rails/LexicallyScopedActionFilter: update, cancel are not explicitly defined on the controller.
  before_action(:authenticate, only: %i[update cancel]) unless foo
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

Use this test class in the /controllers directory

# frozen_string_literal: true

class Test < ActionController
  before_action(:authenticate, only: %i[update cancel]) unless foo

  def update; end

  def cancel; end
end

RuboCop version

$ rubocop -V
0.52.1 (using Parser 2.4.0.2, running on ruby 2.5.0 x86_64-linux)
bug

Most helpful comment

What about filters inserted via mixins? Will they be addressed too when this issue is resolved?
In those cases, the parent is a module instead of a class:

module FooMixin
  extend ActiveSupport::Concern

  included do
    before_action proc { authenticate },
      only: :foo 
  end

  def foo; end
end

class BarController < ActionController
  include FooMixin
end

All 2 comments

Thanks for the bug report!

The cause is here. https://github.com/bbatsov/rubocop/blob/87f453d20f789fb4cb233a661dbed8794502022a/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb#L70
The code expects node.parent is a class node, but the parent is a if node in this case. So we should replace parent with each_ancestor or something.

What about filters inserted via mixins? Will they be addressed too when this issue is resolved?
In those cases, the parent is a module instead of a class:

module FooMixin
  extend ActiveSupport::Concern

  included do
    before_action proc { authenticate },
      only: :foo 
  end

  def foo; end
end

class BarController < ActionController
  include FooMixin
end
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mlammers picture mlammers  路  3Comments

NobodysNightmare picture NobodysNightmare  路  3Comments

kirrmann picture kirrmann  路  3Comments

printercu picture printercu  路  3Comments

benoittgt picture benoittgt  路  3Comments