Skip to content

Commit

Permalink
Fix NoMethodError when calling anonymise_for! with no anonymise config
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabby committed Oct 11, 2024
1 parent 9156b93 commit bbfefa2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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?` on a model class without an `anonymise` config block
- Fix `NoMethodError` when calling `selector_for?` or `anonymise_for!` on a model class without an `anonymise` config block

# v1.4.0

Expand Down
4 changes: 4 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 Down
36 changes: 31 additions & 5 deletions spec/anony/anonymisable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,45 @@ def some_instance_method?
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

0 comments on commit bbfefa2

Please sign in to comment.