Skip to content

Commit 3e76a21

Browse files
authored
4739 Prevent deletion of product drives with donations (#5303)
* feat: prevent deletion of ProductDrives with donations * test: add request spec to prevent deletion of product drives with donations * change: use model error message for product drive deletion failure
1 parent 11ba41f commit 3e76a21

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

app/controllers/product_drives_controller.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,18 @@ def update
8888
end
8989

9090
def destroy
91-
current_organization.product_drives.find(params[:id]).destroy
91+
product_drive = current_organization.product_drives.find(params[:id])
92+
success = product_drive.destroy
93+
9294
respond_to do |format|
93-
format.html { redirect_to product_drives_url, notice: 'Product drive was successfully destroyed.' }
94-
format.json { head :no_content }
95+
if success
96+
format.html { redirect_to product_drives_url, notice: 'Product drive was successfully destroyed.' }
97+
format.json { head :no_content }
98+
else
99+
error_message = product_drive.errors.full_messages.to_sentence
100+
format.html { redirect_to product_drive_path(product_drive), error: error_message }
101+
format.json { render json: {error: error_message}, status: :unprocessable_entity }
102+
end
95103
end
96104
end
97105

app/models/product_drive.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ProductDrive < ApplicationRecord
3131
search_dates[:end_date])
3232
}
3333

34-
has_many :donations, dependent: :nullify
34+
has_many :donations, dependent: :restrict_with_error
3535
has_many :product_drive_participants, -> { distinct }, through: :donations
3636
validates :name, presence:
3737
{ message: "A name must be chosen." }

spec/requests/product_drives_requests_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,22 @@
294294
end
295295

296296
describe "DELETE #destroy" do
297-
it "redirects to the index" do
298-
product_drive = create(:product_drive, organization: organization)
297+
let(:product_drive) { create(:product_drive, organization: organization) }
299298

299+
it "redirects to the index" do
300300
delete product_drive_path(id: product_drive.id)
301301
expect(response).to redirect_to(product_drives_path)
302302
end
303+
304+
context "when the product drive has associated donations" do
305+
it "does not delete and redirects with an error" do
306+
create(:donation, product_drive: product_drive)
307+
308+
delete product_drive_path(id: product_drive.id)
309+
expect(response).to redirect_to(product_drive_path(product_drive))
310+
expect(flash[:error]).to eq("Cannot delete record because dependent donations exist")
311+
end
312+
end
303313
end
304314
end
305315
end

0 commit comments

Comments
 (0)