forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathignored_columns_assignment.rb
50 lines (47 loc) · 1.31 KB
/
ignored_columns_assignment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# 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.
#
# @example
#
# # 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
#
class IgnoredColumnsAssignment < Base
extend AutoCorrector
MSG = 'Use `+=` instead of `=`.'
RESTRICT_ON_SEND = %i[ignored_columns=].freeze
def on_send(node)
add_offense(node.loc.operator) do |corrector|
corrector.replace(node.loc.operator, '+=')
end
end
end
end
end
end