Skip to content
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

Resolves #4739 #4763

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/product_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def self.search_date_range(dates)
@search_date_range = { start_date: dates[0], end_date: dates[1] }
end

def can_delete?(user)
user.has_role?(Role::ORG_ADMIN, organization) && donations.empty?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

end

# quantities are FILTERED by date then SORTED by name
#
# @param date_range [Range]
Expand Down
6 changes: 5 additions & 1 deletion app/views/product_drives/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@
</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) %>
<% if @product_drive.can_delete?(current_user) %>
<%= delete_button_to product_drive_path(@product_drive),
confirm: "Are you sure you want to permanently remove this product drive?",
size: "md" %>
<% end %>
</div>
<!-- /.card-footer-->
</div>
Expand Down
30 changes: 30 additions & 0 deletions spec/system/product_drive_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,34 @@
expect(page).to have_content 'Endless drive'
end
end

context "when deleting a Product drive" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move these tests to request tests? There's no reason to load a whole browser to test the HTML output by the controller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thanks for the heads-up

let(:new_product_drive) { create(:product_drive, name: 'New Test', start_date: 3.weeks.ago, end_date: 3.weeks.from_now, organization: organization) }
let(:subject) { product_drive_path(new_product_drive.id) }

context "when user is a org_admin" do
before do
user.add_role(:org_admin, organization)
end

it "must delete" do
visit subject
click_on 'Delete'
expect(page).to have_content 'Product drive was successfully destroyed.'
end

it "must not delete if there are donations" do
create(:donation, product_drive: new_product_drive)
visit subject
expect(page).not_to have_button 'Delete'
end
end

context "when user is not a org_admin" do
it "must not delete" do
visit subject
expect(page).not_to have_button 'Delete'
end
end
end
end