Skip to content

Commit

Permalink
4294 Disable deleting purchases and donations with inactive items (#4302
Browse files Browse the repository at this point in the history
)

* Issue 4294 - Add check for inactive items in purchases and donations

* Issue-4294 - Update donations and purchases request specs to check for disabled delete button attribute.

* Issue-4294 - Address linting issues.

* Item-4294 - Address PR comment and moved the if for the delete button into block format for donations and purchases.

* Item-4294 - Address linting failure.

* Issue-4294 - Update donation/purchase warning message

* Issue-4294 - Fix formatting issue.

---------

Co-authored-by: Daniel Orner <[email protected]>
  • Loading branch information
pshong79 and dorner authored May 2, 2024
1 parent 8ce0db4 commit e68403a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
11 changes: 8 additions & 3 deletions app/views/donations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@
enabled: !@donation.has_inactive_item?,
size: "md" } %>
<%= new_button_to new_distribution_path(donation_id: @donation.id, storage_location_id: @donation.storage_location_id), { text: "Start a new Distribution" } %>
<%= delete_button_to donation_path(@donation), { size: "md", confirm: "Are you sure you want to permanently remove this donation?" } if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
<%= delete_button_to donation_path(@donation), {
size: "md",
enabled: !@donation.has_inactive_item?,
confirm: "Are you sure you want to permanently remove this donation?" } %>
<% end %>
<% if @donation.has_inactive_item? %>
<div class="warning">
You can only correct donations where all the items are active.
If you need to make a correction, please make the following items active: <%= @donation.inactive_items.map(&:name).join(", ") %>
You can only delete or correct donations where all the items are active.
If you need to delete this donation or make a correction, please make the following items active: <%= @donation.inactive_items.map(&:name).join(", ") %>
</div>
<% end %>
</div>
Expand Down
11 changes: 8 additions & 3 deletions app/views/purchases/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@
<%= edit_button_to edit_purchase_path(@purchase), { text: "Make a correction",
enabled: !@purchase.has_inactive_item?,
size: "md" } %>
<%= delete_button_to purchase_path(@purchase), { size: "md", confirm: "Are you sure you want to permanently remove this purchase?" } if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
<% if current_user.has_role?(Role::ORG_ADMIN, current_organization) %>
<%= delete_button_to purchase_path(@purchase), {
size: "md",
enabled: !@purchase.has_inactive_item?,
confirm: "Are you sure you want to permanently remove this purchase?" } %>
<% end %>
<% if @purchase.has_inactive_item? %>
<div class="warning">
You can only correct purchases where all the items are active.
If you need to make a correction, please make the following items active: <%= @purchase.inactive_items.map(&:name).join(", ") %>
You can only delete or correct purchases where all the items are active.
If you need to delete this purchase or make a correction, please make the following items active: <%= @purchase.inactive_items.map(&:name).join(", ") %>
</div>
<% end %>
</div>
Expand Down
19 changes: 18 additions & 1 deletion spec/requests/donations_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
expect(response.body).not_to match(/please make the following items active:/)
end

context "with an inactive item" do
context "with an inactive item - non organization admin user" do
before do
item.update(active: false)
end
Expand All @@ -108,6 +108,23 @@
expect(response.body).to match(/please make the following items active: #{item.name}/)
end
end

context "with an inactive item - organization admin user" do
before do
sign_in(@organization_admin)
item.update(active: false)
end

it "shows a disabled edit and delete buttons" do
get donation_path(default_params.merge(id: donation.id))
page = Nokogiri::HTML(response.body)
edit = page.at_css("a[href='#{edit_donation_path(default_params.merge(id: donation.id))}']")
delete = page.at_css("a.btn-danger[href='#{donation_path(default_params.merge(id: donation.id))}']")
expect(edit.attr("class")).to match(/disabled/)
expect(delete.attr("class")).to match(/disabled/)
expect(response.body).to match(/please make the following items active: #{item.name}/)
end
end
end

describe "GET #edit" do
Expand Down
19 changes: 18 additions & 1 deletion spec/requests/purchases_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
expect(response.body).not_to match(/please make the following items active:/)
end

context "with an inactive item" do
context "with an inactive item - non-organization admin user" do
before do
item.update(active: false)
end
Expand All @@ -273,6 +273,23 @@
expect(response.body).to match(/please make the following items active: #{item.name}/)
end
end

context "with an inactive item - organization admin user" do
before do
sign_in(@organization_admin)
item.update(active: false)
end

it "shows a disabled edit and delete buttons" do
get purchase_path(default_params.merge(id: purchase.id))
page = Nokogiri::HTML(response.body)
edit = page.at_css("a[href='#{edit_purchase_path(default_params.merge(id: purchase.id))}']")
delete = page.at_css("a.btn-danger[href='#{purchase_path(default_params.merge(id: purchase.id))}']")
expect(edit.attr("class")).to match(/disabled/)
expect(delete.attr("class")).to match(/disabled/)
expect(response.body).to match(/please make the following items active: #{item.name}/)
end
end
end

describe "DELETE #destroy" do
Expand Down

0 comments on commit e68403a

Please sign in to comment.