From 2e96c7a19b162900a9a46795b938581e7857aa30 Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Wed, 13 Mar 2019 10:48:06 -0400 Subject: [PATCH] Don't allow deletion for users with orders --- .../app/views/spree/admin/users/index.html.erb | 2 +- backend/spec/features/admin/users_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/backend/app/views/spree/admin/users/index.html.erb b/backend/app/views/spree/admin/users/index.html.erb index de948adfc6c..ae828516b52 100644 --- a/backend/app/views/spree/admin/users/index.html.erb +++ b/backend/app/views/spree/admin/users/index.html.erb @@ -89,7 +89,7 @@ <% if can?(:edit, user) %> <%= link_to_edit user, no_text: true %> <% end %> - <% if can?(:destroy, user) %> + <% if can?(:destroy, user) && user.orders.count.zero? %> <%= link_to_delete user, no_text: true %> <% end %> diff --git a/backend/spec/features/admin/users_spec.rb b/backend/spec/features/admin/users_spec.rb index 0fc32c5de04..7d9bafddb32 100644 --- a/backend/spec/features/admin/users_spec.rb +++ b/backend/spec/features/admin/users_spec.rb @@ -289,6 +289,24 @@ def always_invalid_email end end + context 'deleting users' do + let!(:an_user) { create(:user_with_addresses, email: 'an_user@example.com') } + let!(:order) { create(:completed_order_with_totals, user_id: an_user.id) } + + context 'if an user has placed orders' do + before do + visit spree.admin_path + click_link 'Users' + end + + it "can't be deleted" do + within "#spree_user_#{an_user.id}" do + expect(page).not_to have_selector('.fa-trash') + end + end + end + end + context 'order history with sorting' do before do orders