debian-mirror-gitlab/rubocop/cop/ignored_columns.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.6 KiB
Ruby
Raw Normal View History

2020-01-01 13:55:28 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
2021-10-27 15:23:28 +05:30
# Cop that blacklists the usage of `ActiveRecord::Base.ignored_columns=` directly
2022-10-11 01:57:18 +05:30
class IgnoredColumns < RuboCop::Cop::Base
2021-10-27 15:23:28 +05:30
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.'
2020-01-01 13:55:28 +05:30
def_node_matcher :ignored_columns?, <<~PATTERN
(send (self) :ignored_columns)
PATTERN
2021-10-27 15:23:28 +05:30
def_node_matcher :ignore_columns?, <<~PATTERN
(send nil? :ignore_columns ...)
PATTERN
def_node_matcher :ignore_column?, <<~PATTERN
(send nil? :ignore_column ...)
PATTERN
2020-01-01 13:55:28 +05:30
def on_send(node)
2021-10-27 15:23:28 +05:30
if ignored_columns?(node)
2022-10-11 01:57:18 +05:30
add_offense(node, message: USE_CONCERN_MSG)
2021-10-27 15:23:28 +05:30
end
if using_ignore?(node) && used_in_wrong_model?
2022-10-11 01:57:18 +05:30
add_offense(node, message: WRONG_MODEL_MSG)
2021-10-27 15:23:28 +05:30
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
2020-01-01 13:55:28 +05:30
2021-10-27 15:23:28 +05:30
def ce_model_exists?(path)
File.exist?(path.gsub(%r{/ee/}, '/'))
2020-01-01 13:55:28 +05:30
end
end
end
end