diff --git a/.rubocop.yml b/.rubocop.yml index d1393b1..f016d4b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,6 +9,9 @@ AllCops: Layout/LineLength: Max: 100 +Gemspec/DevelopmentDependencies: + EnforcedStyle: gemspec + Gemspec/RequiredRubyVersion: Enabled: false diff --git a/README.md b/README.md index f14ed62..3beb9ef 100644 --- a/README.md +++ b/README.md @@ -489,6 +489,15 @@ Lint/DefineDeletionStrategy: ModelSuperclass: Acme::Record ``` +If your models use multiple superclasses, you can specify a list of superclasses in your `.rubocop.yml`. Note that you will have to specify `ApplicationRecord` explicitly in this list should you want to lint all models which inherit from `ApplicationRecord`. +```yml +Lint/DefineDeletionStrategy: + ModelSuperclass: + - Acme::Record + - UmbrellaCorp::Record + +``` + ## License & Contributing * Anony is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). diff --git a/lib/anony/anonymisable.rb b/lib/anony/anonymisable.rb index eb704e7..cb5c334 100644 --- a/lib/anony/anonymisable.rb +++ b/lib/anony/anonymisable.rb @@ -2,8 +2,8 @@ require "active_support/core_ext/module/delegation" -require_relative "./not_anonymisable_exception" -require_relative "./strategies/overwrite" +require_relative "not_anonymisable_exception" +require_relative "strategies/overwrite" require_relative "model_config" module Anony diff --git a/lib/anony/cops/define_deletion_strategy.rb b/lib/anony/cops/define_deletion_strategy.rb index 60db0b6..4a9ad02 100644 --- a/lib/anony/cops/define_deletion_strategy.rb +++ b/lib/anony/cops/define_deletion_strategy.rb @@ -36,9 +36,8 @@ def on_class(node) end def model?(node) - return unless (superclass = node.children[1]) - - superclass.const_name == model_superclass_name + superclass = node.children[1] + model_superclass_name.include? superclass&.const_name end def class_name(node) @@ -46,7 +45,15 @@ def class_name(node) end def model_superclass_name - cop_config["ModelSuperclass"] || "ApplicationRecord" + unless cop_config["ModelSuperclass"] + return ["ApplicationRecord"] + end + + if cop_config["ModelSuperclass"].is_a?(Array) + return cop_config["ModelSuperclass"] + end + + [cop_config["ModelSuperclass"]] end end end diff --git a/lib/anony/model_config.rb b/lib/anony/model_config.rb index cdfd04f..9cf4eca 100644 --- a/lib/anony/model_config.rb +++ b/lib/anony/model_config.rb @@ -2,9 +2,9 @@ require "active_support/core_ext/module/delegation" -require_relative "./strategies/destroy" -require_relative "./strategies/overwrite" -require_relative "./selectors" +require_relative "strategies/destroy" +require_relative "strategies/overwrite" +require_relative "selectors" module Anony class ModelConfig @@ -108,7 +108,7 @@ def selectors(&block) end def selector_for?(subject) - return nil if @selectors_config.nil? + return false if @selectors_config.nil? @selectors_config.selectors[subject].present? end diff --git a/lib/anony/selectors.rb b/lib/anony/selectors.rb index 358c052..f2e849c 100644 --- a/lib/anony/selectors.rb +++ b/lib/anony/selectors.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative "./selector_not_found_exception" +require_relative "selector_not_found_exception" module Anony class Selectors diff --git a/lib/anony/version.rb b/lib/anony/version.rb index 798c8f9..72134d3 100644 --- a/lib/anony/version.rb +++ b/lib/anony/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Anony - VERSION = "1.1.0" + VERSION = "1.2.0" end diff --git a/spec/anony/cops/define_deletion_strategy_spec.rb b/spec/rubocop/cop/lint/define_deletion_strategy_spec.rb similarity index 68% rename from spec/anony/cops/define_deletion_strategy_spec.rb rename to spec/rubocop/cop/lint/define_deletion_strategy_spec.rb index 4235f0b..13dc159 100644 --- a/spec/anony/cops/define_deletion_strategy_spec.rb +++ b/spec/rubocop/cop/lint/define_deletion_strategy_spec.rb @@ -94,4 +94,48 @@ class Employee < Acme::Record it_behaves_like "an offense" end end + + context "when it uses multiple super classes" do + subject(:offenses) { cop.offenses } + + let(:cop_config) { { "ModelSuperclass" => ["Acme::Record", "Another::Record"] } } + + context "when models defines anonymisation rules" do + let(:source) do + <<~RUBY + class Employee < Acme::Record + anonymise do + destroy + end + end + + class Boss < Another::Record + anonymise do + destroy + end + end + RUBY + end + + it { expect(cop.offenses).to be_empty } + end + + context "when models are missing anonymisation rules" do + let(:source) do + <<~RUBY + class Employee < Another::Record + end + + class Boss < Another::Record + end + RUBY + end + + it { expect(offenses.count).to eq(2) } + + it "has the correct name" do + expect(offenses.first.cop_name).to eq(cop.name) + end + end + end end