diff --git a/backend/app/views/spree/admin/users/index.html.erb b/backend/app/views/spree/admin/users/index.html.erb index 15762cdddfa..b526aaa24ad 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) && user.orders.none? %> + <% if can?(:destroy, user) && user.can_be_deleted? %> <%= link_to_delete user, no_text: true %> <% end %> diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rb index 533b15b5c7a..bd54afd07b7 100644 --- a/core/app/models/concerns/spree/user_methods.rb +++ b/core/app/models/concerns/spree/user_methods.rb @@ -18,7 +18,7 @@ module UserMethods has_many :stock_locations, through: :user_stock_locations has_many :spree_orders, foreign_key: "user_id", class_name: "Spree::Order" - has_many :orders, foreign_key: "user_id", class_name: "Spree::Order", dependent: :restrict_with_exception + has_many :orders, foreign_key: "user_id", class_name: "Spree::Order" has_many :store_credits, -> { includes(:credit_type) }, foreign_key: "user_id", class_name: "Spree::StoreCredit" has_many :store_credit_events, through: :store_credits @@ -27,6 +27,7 @@ module UserMethods has_many :wallet_payment_sources, foreign_key: 'user_id', class_name: 'Spree::WalletPaymentSource', inverse_of: :user after_create :auto_generate_spree_api_key + before_destroy :check_for_deletion include Spree::RansackableAttributes unless included_modules.include?(Spree::RansackableAttributes) @@ -76,5 +77,23 @@ def display_available_store_credit_total(currency:) currency: currency, ) end + + # Restrict to delete users with existing orders + # + # Override this in your user model class to add another logic. + # + # Ie. to allow to delete users with incomplete orders add: + # + # orders.complete.none? + # + def can_be_deleted? + orders.none? + end + + private + + def check_for_deletion + raise ActiveRecord::DeleteRestrictionError unless can_be_deleted? + end end end