diff --git a/app/services/condition_repository.rb b/app/services/condition_repository.rb index 2bd702bd1..15dd9857f 100644 --- a/app/services/condition_repository.rb +++ b/app/services/condition_repository.rb @@ -29,7 +29,14 @@ def save!(record) def destroy(record) condition = Api::V1::ConditionResource.new(record.attributes, true) condition.prefix_options = record.prefix_options - condition.destroy # rubocop:disable Rails/SaveBang + + begin + condition.destroy # rubocop:disable Rails/SaveBang + rescue ActiveResource::ResourceNotFound + # ActiveRecord::Persistence#destroy doesn't raise an error + # if record has already been destroyed, let's emulate that + end + record end end diff --git a/app/services/form_repository.rb b/app/services/form_repository.rb index dda0634b0..6f5cdaabd 100644 --- a/app/services/form_repository.rb +++ b/app/services/form_repository.rb @@ -40,7 +40,14 @@ def archive!(record) def destroy(record) form = Api::V1::FormResource.new(record.attributes, true) - form.destroy # rubocop:disable Rails/SaveBang + + begin + form.destroy # rubocop:disable Rails/SaveBang + rescue ActiveResource::ResourceNotFound + # ActiveRecord::Persistence#destroy doesn't raise an error + # if record has already been destroyed, let's emulate that + end + record end diff --git a/app/services/page_repository.rb b/app/services/page_repository.rb index 05c54fbd9..c8621ba67 100644 --- a/app/services/page_repository.rb +++ b/app/services/page_repository.rb @@ -34,7 +34,14 @@ def save!(record) def destroy(record) page = Api::V1::PageResource.new(record.attributes, true) page.prefix_options = record.prefix_options - page.destroy # rubocop:disable Rails/SaveBang + + begin + page.destroy # rubocop:disable Rails/SaveBang + rescue ActiveResource::ResourceNotFound + # ActiveRecord::Persistence#destroy doesn't raise an error + # if record has already been destroyed, let's emulate that + end + record end diff --git a/spec/services/condition_repository_spec.rb b/spec/services/condition_repository_spec.rb index e0ce719f3..492da657b 100644 --- a/spec/services/condition_repository_spec.rb +++ b/spec/services/condition_repository_spec.rb @@ -77,5 +77,31 @@ condition = described_class.find(condition_id: 4, form_id: 1, page_id: 2) expect(described_class.destroy(condition)).to eq condition end + + context "when the condition has already been deleted" do + it "does not raise an error" do + condition = described_class.find(condition_id: 4, form_id: 1, page_id: 2) + described_class.destroy(condition) + + ActiveResource::HttpMock.respond_to do |mock| + mock.delete "/api/v1/forms/1/pages/2/conditions/4", delete_headers, nil, 404 + end + + expect { + described_class.destroy(condition) + }.not_to raise_error + end + + it "returns the deleted condition" do + condition = described_class.find(condition_id: 4, form_id: 1, page_id: 2) + described_class.destroy(condition) + + ActiveResource::HttpMock.respond_to do |mock| + mock.delete "/api/v1/forms/1/pages/2/conditions/4", delete_headers, nil, 404 + end + + expect(described_class.destroy(condition)).to eq condition + end + end end end diff --git a/spec/services/form_repository_spec.rb b/spec/services/form_repository_spec.rb index 0d48112c4..add82e3c3 100644 --- a/spec/services/form_repository_spec.rb +++ b/spec/services/form_repository_spec.rb @@ -149,6 +149,32 @@ form = described_class.find(form_id: 2) expect(described_class.destroy(form)).to eq form end + + context "when the form has already been deleted" do + it "does not raise an error" do + form = described_class.find(form_id: 2) + described_class.destroy(form) + + ActiveResource::HttpMock.respond_to do |mock| + mock.delete "/api/v1/forms/2", delete_headers, nil, 404 + end + + expect { + described_class.destroy(form) + }.not_to raise_error + end + + it "returns the deleted form" do + form = described_class.find(form_id: 2) + described_class.destroy(form) + + ActiveResource::HttpMock.respond_to do |mock| + mock.delete "/api/v1/forms/2", delete_headers, nil, 404 + end + + expect(described_class.destroy(form)).to eq form + end + end end describe "#pages" do diff --git a/spec/services/page_repository_spec.rb b/spec/services/page_repository_spec.rb index a7e610b59..110065ffb 100644 --- a/spec/services/page_repository_spec.rb +++ b/spec/services/page_repository_spec.rb @@ -79,6 +79,32 @@ page = described_class.find(page_id: 2, form_id: 1) expect(described_class.destroy(page)).to eq page end + + context "when the page has already been deleted" do + it "does not raise an error" do + page = described_class.find(page_id: 2, form_id: 1) + described_class.destroy(page) + + ActiveResource::HttpMock.respond_to do |mock| + mock.delete "/api/v1/forms/1/pages/2", delete_headers, nil, 404 + end + + expect { + described_class.destroy(page) + }.not_to raise_error + end + + it "returns the deleted page" do + page = described_class.find(page_id: 2, form_id: 1) + described_class.destroy(page) + + ActiveResource::HttpMock.respond_to do |mock| + mock.delete "/api/v1/forms/1/pages/2", delete_headers, nil, 404 + end + + expect(described_class.destroy(page)).to eq page + end + end end describe "#move_page" do