diff --git a/app/controllers/places_controller.rb b/app/controllers/places_controller.rb index 2891a10db26..951995d3b44 100644 --- a/app/controllers/places_controller.rb +++ b/app/controllers/places_controller.rb @@ -216,7 +216,10 @@ def update def destroy errors = [] - errors << "there are people using this place in their projects" if @place.projects.exists? + if @place.projects.exists? || + ProjectObservationRule.where(ruler_type: 'Project', operand: @place, operator: "observed_in_place?").exists? + errors << "there are people using this place in their projects" + end errors << "there are people using this place in their guides" if @place.guides.exists? if errors.blank? @place.destroy diff --git a/spec/controllers/places_controller_spec.rb b/spec/controllers/places_controller_spec.rb index 5974df62105..cf69ca4f135 100644 --- a/spec/controllers/places_controller_spec.rb +++ b/spec/controllers/places_controller_spec.rb @@ -18,13 +18,26 @@ describe "destroy" do let(:user) { User.make! } let(:place) { Place.make!(:user => user) } - it "should delete the place" do + before do sign_in user + end + it "should delete the place" do expect(place).not_to be_blank expect { delete :destroy, :id => place.id }.to change(Place, :count).by(-1) end + it "should fail if projects are using the place" do + p = Project.make!(place: place) + delete :destroy, id: place.id + expect( Place.find_by_id(place.id) ).not_to be_blank + end + it "should fail if projects are using the place in rules" do + p = Project.make! + p.project_observation_rules.create(operand: place, operator: 'observed_in_place?') + delete :destroy, id: place.id + expect( Place.find_by_id(place.id) ).not_to be_blank + end end describe "search" do