Skip to content

Commit

Permalink
Support multiple superclasses (#98)
Browse files Browse the repository at this point in the history
* Add ability to use multiple superclasses

* clarify multiple classes rule in readme

* Fix rubocop offenses

* remove dependency on Array.wrap

* lint: fix line length

* lint: use guard style

* Revert dependency cfg change & tweak rubocop config

---------

Co-authored-by: Tabby Cromarty <[email protected]>
  • Loading branch information
mfmmq and Tabby authored Feb 20, 2024
1 parent b6465ab commit 606a664
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ AllCops:
Layout/LineLength:
Max: 100

Gemspec/DevelopmentDependencies:
EnforcedStyle: gemspec

Gemspec/RequiredRubyVersion:
Enabled: false

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions lib/anony/anonymisable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions lib/anony/cops/define_deletion_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,24 @@ 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)
node.children[0].const_name
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
Expand Down
8 changes: 4 additions & 4 deletions lib/anony/model_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/anony/selectors.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "./selector_not_found_exception"
require_relative "selector_not_found_exception"

module Anony
class Selectors
Expand Down
2 changes: 1 addition & 1 deletion lib/anony/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Anony
VERSION = "1.1.0"
VERSION = "1.2.0"
end
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 606a664

Please sign in to comment.