Class: RuboCop::Cop::Rails::IgnoredColumnsAssignment

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/ignored_columns_assignment.rb

Overview

Looks for assignments of ‘ignored_columns` that may override previous assignments.

Overwriting previous assignments is usually a mistake, since it will un-ignore the first set of columns. Since duplicate column names is not a problem, it is better to simply append to the list.

Examples:


# bad
class User < ActiveRecord::Base
  self.ignored_columns = [:one]
end

# bad
class User < ActiveRecord::Base
  self.ignored_columns = [:one, :two]
end

# good
class User < ActiveRecord::Base
  self.ignored_columns += [:one, :two]
end

# good
class User < ActiveRecord::Base
  self.ignored_columns += [:one]
  self.ignored_columns += [:two]
end

Constant Summary collapse

MSG =
'Use `+=` instead of `=`.'
RESTRICT_ON_SEND =
%i[ignored_columns=].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



42
43
44
45
46
# File 'lib/rubocop/cop/rails/ignored_columns_assignment.rb', line 42

def on_send(node)
  add_offense(node.loc.operator) do |corrector|
    corrector.replace(node.loc.operator, '+=')
  end
end