summaryrefslogtreecommitdiff
path: root/rubocop
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-08-05 09:08:56 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-08-05 09:08:56 +0000
commit258cd2409347baa19566d8bf34207521c142d8b7 (patch)
treea3c7bb6cdc354abd647e223148c1d6b8b432cde8 /rubocop
parent962fbcfb94b13668632de822e3f7a74fb5ecaf68 (diff)
downloadgitlab-ce-258cd2409347baa19566d8bf34207521c142d8b7.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r--rubocop/cop/ignored_columns.rb50
1 files changed, 46 insertions, 4 deletions
diff --git a/rubocop/cop/ignored_columns.rb b/rubocop/cop/ignored_columns.rb
index 14bcfa04ae1..4a6f1e4f2d9 100644
--- a/rubocop/cop/ignored_columns.rb
+++ b/rubocop/cop/ignored_columns.rb
@@ -2,18 +2,60 @@
module RuboCop
module Cop
- # Cop that blacklists the usage of Group.public_or_visible_to_user
+ # Cop that blacklists the usage of `ActiveRecord::Base.ignored_columns=` directly
class IgnoredColumns < RuboCop::Cop::Cop
- MSG = 'Use `IgnoredColumns` concern instead of adding to `self.ignored_columns`.'
+ USE_CONCERN_MSG = 'Use `IgnoredColumns` concern instead of adding to `self.ignored_columns`.'
+ WRONG_MODEL_MSG = 'If the model exists in CE and EE, the column has to be ignored ' \
+ 'in the CE model. If the model only exists in EE, then it has to be added there.'
def_node_matcher :ignored_columns?, <<~PATTERN
(send (self) :ignored_columns)
PATTERN
+ def_node_matcher :ignore_columns?, <<~PATTERN
+ (send nil? :ignore_columns ...)
+ PATTERN
+
+ def_node_matcher :ignore_column?, <<~PATTERN
+ (send nil? :ignore_column ...)
+ PATTERN
+
def on_send(node)
- return unless ignored_columns?(node)
+ if ignored_columns?(node)
+ add_offense(node, location: :expression, message: USE_CONCERN_MSG)
+ end
+
+ if using_ignore?(node) && used_in_wrong_model?
+ add_offense(node, location: :expression, message: WRONG_MODEL_MSG)
+ end
+ end
+
+ private
+
+ def using_ignore?(node)
+ ignore_columns?(node) || ignore_column?(node)
+ end
+
+ def used_in_wrong_model?
+ file_path = processed_source.file_path
+
+ ee_model?(file_path) && ce_model_exists?(file_path)
+ end
+
+ def ee_model?(path)
+ path.include?(ee_directory)
+ end
+
+ def ee_directory
+ File.join(rails_root, 'ee')
+ end
+
+ def rails_root
+ File.expand_path('../..', __dir__)
+ end
- add_offense(node, location: :expression)
+ def ce_model_exists?(path)
+ File.exist?(path.gsub(%r{/ee/}, '/'))
end
end
end