Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NoMethodError when calling selector methods w/o anony config #113

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Drop support for EOL Rails 6.1 (EOL since October 1st 2024)
- Add support for Rails 7.2
- Fix Dependabot updates
- Fix `NoMethodError` when calling `selector_for?` or `anonymise_for!` on a model class without an `anonymise` config block

# v1.4.0

Expand Down
6 changes: 6 additions & 0 deletions lib/anony/anonymisable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def valid_anonymisation?
# Finds the records that relate to a particular subject and runs anonymise on
# each of them. If a selector is not defined it will raise an exception.
def anonymise_for!(subject, subject_id)
unless anonymise_config
raise ArgumentError, "#{name} does not have an Anony configuration"
end

records = anonymise_config.
select(subject, subject_id)
records.map do |record|
Expand All @@ -76,6 +80,8 @@ def anonymise_for!(subject, subject_id)
# @example
# Manager.selector_for?(:user_id)
def selector_for?(subject)
return false if anonymise_config.nil?

anonymise_config.selector_for?(subject)
end

Expand Down
55 changes: 47 additions & 8 deletions spec/anony/anonymisable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,38 +221,77 @@ def some_instance_method?

context "without configuring Anony at all" do
let(:klass) do
MyUnicornModel = Class.new(ActiveRecord::Base) do
Class.new(ActiveRecord::Base) do
include Anony::Anonymisable

self.table_name = :only_ids
end
end

let(:model) { klass.new }

describe "#anonymise!" do
let(:model) do
MyUnicornModel = klass
MyUnicornModel.new
end

it "throws an exception" do
expect { model.anonymise! }.to raise_error(
ArgumentError, "MyUnicornModel does not have an Anony configuration"
)
end
end

describe "#selector_for?" do
it "does not throw an exception" do
expect { klass.selector_for?(:foo) }.to_not raise_error
end

it "returns false" do
expect(klass.selector_for?(:foo)).to be false
end
end
end

context "no anonymise block" do
describe "#valid_anonymisation?" do
let(:klass) do
Class.new(ActiveRecord::Base) do
include Anony::Anonymisable
let(:klass) do
Class.new(ActiveRecord::Base) do
include Anony::Anonymisable

self.table_name = :employees
def self.name
"Employee"
end

self.table_name = :employees
end
end

describe "#valid_anonymisation?" do
it "fails" do
expect(klass).to_not be_valid_anonymisation
end
end

describe "#selector_for?" do
it "does not throw an exception" do
expect { klass.selector_for?(:foo) }.to_not raise_error
end

it "returns false" do
expect(klass.selector_for?(:foo)).to be false
end
end

describe "#anonymise_for!" do
let(:model) do
klass.create!(first_name: "abc", last_name: "foo", company_name: "alpha")
end

it "throws an exception" do
expect { klass.anonymise_for!(:first_name, "abc") }.to raise_error(
ArgumentError, "Employee does not have an Anony configuration"
)
end
end
end

context "with an empty anonymise block" do
Expand Down
Loading