Skip to content

Commit 6571afe

Browse files
Don't allow deleting of organisations with vacancies
1 parent 21f879e commit 6571afe

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

app/models/organisation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Organisation < ApplicationRecord
1515
has_one_attached :logo, service: :azure_storage_images_and_logos
1616
has_one_attached :photo, service: :azure_storage_images_and_logos
1717

18-
has_many :organisation_vacancies, dependent: :destroy
18+
has_many :organisation_vacancies, dependent: :restrict_with_error
1919
has_many :vacancies, through: :organisation_vacancies
2020
has_many :publisher_vacancies, class_name: "Vacancy", foreign_key: :publisher_organisation_id, dependent: :destroy
2121

spec/models/organisation_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,20 @@
243243
end
244244
end
245245
end
246+
247+
describe "deletion restrictions" do
248+
context "when organisation has vacancies" do
249+
let(:organisation) { create(:school) }
250+
let!(:vacancy) { create(:vacancy, organisations: [organisation]) }
251+
252+
it "prevents deletion" do
253+
expect { organisation.destroy }.not_to change(Organisation, :count)
254+
end
255+
256+
it "adds an error to the organisation" do
257+
organisation.destroy
258+
expect(organisation.errors[:base]).to include("Cannot delete record because dependent organisation vacancies exist")
259+
end
260+
end
261+
end
246262
end

0 commit comments

Comments
 (0)