Commit a2b8422e authored by Andreas Brandl's avatar Andreas Brandl

Discourage direct use of ignored_columns in models

This discourages the use of `self.ignored_columns += ...` in models and
instead suggests to use `IgnorableColumns` concern and `ignore_column`
methods provided.
parent 2c9af53a
......@@ -21,7 +21,7 @@ module IgnorableColumns
raise ArgumentError, 'Please indicate when we can stop ignoring columns with remove_after (date string), example: ignore_columns(:name, remove_after: \'2019-12-01\', remove_with: \'12.6\')' unless remove_after
raise ArgumentError, 'Please indicate in which release we can stop ignoring columns with remove_with, example: ignore_columns(:name, remove_after: \'2019-12-01\', remove_with: \'12.6\')' unless remove_with
self.ignored_columns += columns.flatten
self.ignored_columns += columns.flatten # rubocop:disable Cop/IgnoredColumns
unless defined?(@ignored_columns_details)
@ignored_columns_details = superclass.try(:ignored_columns_details)&.dup || {}
......
# frozen_string_literal: true
module RuboCop
module Cop
# Cop that blacklists the usage of Group.public_or_visible_to_user
class IgnoredColumns < RuboCop::Cop::Cop
MSG = 'Use `IgnoredColumns` concern instead of adding to `self.ignored_columns`.'
def_node_matcher :ignored_columns?, <<~PATTERN
(send (self) :ignored_columns)
PATTERN
def on_send(node)
return unless ignored_columns?(node)
add_offense(node, location: :expression)
end
end
end
end
......@@ -54,3 +54,4 @@ require_relative 'cop/group_public_or_visible_to_user'
require_relative 'cop/inject_enterprise_edition_module'
require_relative 'cop/graphql/authorize_types'
require_relative 'cop/graphql/descriptions'
require_relative 'cop/ignored_columns'
# frozen_string_literal: true
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/ignored_columns'
describe RuboCop::Cop::IgnoredColumns do
include CopHelper
subject(:cop) { described_class.new }
it 'flags the use of destroy_all with a local variable receiver' do
inspect_source(<<~RUBY)
class Foo < ApplicationRecord
self.ignored_columns += %i[id]
end
RUBY
expect(cop.offenses.size).to eq(1)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment