Skip to content

Commit

Permalink
Add deprecation warning when creating `Result\' with no record
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabby committed Oct 14, 2024
1 parent a351d01 commit 88e09b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/anony/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Result

delegate :failed?, :overwritten?, :skipped?, :destroyed?, to: :status

RESULT_DEPRECATION = ActiveSupport::Deprecation.new("2.0.0", "anony")

def self.failed(error, record = nil)
new(FAILED, record: record, error: error)
end
Expand All @@ -32,6 +34,13 @@ def self.destroyed(record = nil)
private def initialize(status, record:, fields: [], error: nil)
raise ArgumentError, "No error provided" if status == FAILED && error.nil?

if record.nil?
RESULT_DEPRECATION.warn(
"Creating a Result without a reference to the record being anonymised is deprecated " \
"and will be removed in future versions",
)
end

@status = ActiveSupport::StringInquirer.new(status)
@fields = fields
@error = error
Expand Down
10 changes: 10 additions & 0 deletions spec/anony/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
}
end

shared_context "without model instance" do
before do
allow(described_class::RESULT_DEPRECATION).to receive(:warn)
end
end

shared_context "with model instance" do
let(:klass) do
Class.new(ActiveRecord::Base) do
Expand Down Expand Up @@ -39,6 +45,7 @@ def self.name
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.overwritten(field_values) }

it_behaves_like "anonymised result"
Expand Down Expand Up @@ -72,6 +79,7 @@ def self.name
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.destroyed }

it_behaves_like "destroyed result"
Expand Down Expand Up @@ -105,6 +113,7 @@ def self.name
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.skipped }

it_behaves_like "skipped result"
Expand Down Expand Up @@ -146,6 +155,7 @@ def self.name
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.failed(error) }

it_behaves_like "failed result"
Expand Down

0 comments on commit 88e09b4

Please sign in to comment.