-
-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#4739: Fix product drive delete (updated) #4918
base: main
Are you sure you want to change the base?
Changes from 7 commits
39c7550
3aec94e
cb17ab1
f7b416a
65d9b7e
83976b7
ea47123
3204abe
677de12
b0ac10f
045d8be
018c9f4
1b8c05f
4d822f3
542a1e7
cbb18ec
4bb1407
e8edee3
21cf464
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,8 @@ class ProductDrive < ApplicationRecord | |
|
||
validate :end_date_is_bigger_of_end_date | ||
|
||
before_destroy :validate_destroy, prepend: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this method defined somewhere? |
||
|
||
def end_date_is_bigger_of_end_date | ||
return if start_date.nil? || end_date.nil? | ||
|
||
|
@@ -69,6 +71,10 @@ def self.search_date_range(dates) | |
@search_date_range = { start_date: dates[0], end_date: dates[1] } | ||
end | ||
|
||
def destroy_product_drive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure this method is really worth defining when it's a one-liner. |
||
ProductDriveDestroyService.new(self).call | ||
end | ||
|
||
# quantities are FILTERED by date then SORTED by name | ||
# | ||
# @param date_range [Range] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class ProductDriveDestroyService | ||
def initialize(product_drive) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change |
||
@product_drive = product_drive | ||
end | ||
|
||
def call | ||
return false unless can_destroy? | ||
|
||
# Perform destruction logic | ||
@product_drive.destroy | ||
end | ||
|
||
private | ||
|
||
def can_destroy? | ||
if @product_drive.donations.empty? | ||
true | ||
else | ||
@product_drive.errors.add(:base, "Cannot delete product drive with donations.") | ||
false | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,10 @@ | |
</p> | ||
<div class="card-footer clearfix"> | ||
<%= edit_button_to edit_product_drive_path(@product_drive), { text: "Make a correction", size: "md" } %> | ||
<%= delete_button_to product_drive_path(@product_drive), { confirm: "Are you sure you want to permanently remove this product drive?", size: "md" } if current_user.has_role?(Role::ORG_ADMIN, current_organization) %> | ||
confirm: "Are you sure you want to permanently remove this product drive?", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was a mistake? |
||
size: "md", <%= delete_button_to product_drive_path(@product_drive), | ||
|
||
enabled: ProductDrivePolicy.can_destroy?(@product_drive, current_user) %> | ||
</div> | ||
<!-- /.card-footer--> | ||
</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,11 +209,37 @@ | |
end | ||
|
||
describe "DELETE #destroy" do | ||
it "redirects to the index" do | ||
product_drive = create(:product_drive, organization: organization) | ||
let(:product_drive) { create(:product_drive, organization: organization) } | ||
|
||
context 'when user is not a org_admin' do | ||
it "does not delete the product drive" do | ||
delete product_drive_path(id: product_drive.id) | ||
follow_redirect! | ||
|
||
expect(response.body).to include('You are not allowed to perform this action.') | ||
end | ||
end | ||
|
||
context 'when user is a org_admin' do | ||
before do | ||
user.add_role(Role::ORG_ADMIN, organization) | ||
end | ||
|
||
it 'deletes the product drive' do | ||
delete product_drive_path(id: product_drive.id) | ||
follow_redirect! | ||
|
||
expect(response.body).to include('Product drive was successfully destroyed.') | ||
end | ||
|
||
it "does not delete the product drive if it has donations" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test probably should be moved to the destroy service. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, should I replace it with the code that was previously in (describe "DELETE #destroy") or would you rather I delete it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can leave the code that was there before. |
||
user.add_role(Role::ORG_ADMIN, organization) | ||
create(:donation, product_drive: product_drive) | ||
delete product_drive_path(id: product_drive.id) | ||
follow_redirect! | ||
|
||
delete product_drive_path(id: product_drive.id) | ||
expect(response).to redirect_to(product_drives_path) | ||
expect(response.body).to include('Cannot delete product drive with donations.') | ||
end | ||
end | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this verification to the destroy service? You can pass the user into it.