diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 3c080e7fca..dd5a63a5fc 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -45,25 +45,29 @@ class Enterprise < ApplicationRecord has_many :supplied_variants, through: :supplied_products, source: :variants has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id', - dependent: :destroy + dependent: :restrict_with_exception belongs_to :address, class_name: 'Spree::Address' belongs_to :business_address, optional: true, class_name: 'Spree::Address', dependent: :destroy - has_many :enterprise_fees, dependent: nil # paranoid association is deleted in a before_destroy + has_many :enterprise_fees, dependent: :restrict_with_exception has_many :enterprise_roles, dependent: :destroy has_many :users, through: :enterprise_roles belongs_to :owner, class_name: 'Spree::User', inverse_of: :owned_enterprises has_many :distributor_payment_methods, - inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy + inverse_of: :distributor, + foreign_key: :distributor_id, + dependent: :restrict_with_exception has_many :distributor_shipping_methods, - inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy + inverse_of: :distributor, + foreign_key: :distributor_id, + dependent: :restrict_with_exception has_many :payment_methods, through: :distributor_payment_methods has_many :shipping_methods, through: :distributor_shipping_methods has_many :customers, dependent: :destroy has_many :inventory_items, dependent: :destroy has_many :tag_rules, dependent: :destroy has_one :stripe_account, dependent: :destroy - has_many :vouchers, dependent: nil # paranoid association is deleted in a before_destroy + has_many :vouchers, dependent: :restrict_with_exception has_many :connected_apps, dependent: :destroy has_one :custom_tab, dependent: :destroy @@ -130,8 +134,6 @@ class Enterprise < ApplicationRecord after_create :set_default_contact after_create :relate_to_owners_enterprises - before_destroy :delete_all_enterprise_fees - before_destroy :delete_all_vouchers after_rollback :restore_permalink after_touch :touch_distributors @@ -590,12 +592,4 @@ class Enterprise < ApplicationRecord where.not(enterprises: { id: }). update_all(updated_at: Time.zone.now) end - - def delete_all_enterprise_fees - enterprise_fees.each(&:really_destroy!) - end - - def delete_all_vouchers - vouchers.each(&:really_destroy!) - end end diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index d21514ff8e..8fd7c29f1f 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -57,53 +57,61 @@ describe Enterprise do expect(EnterpriseRelationship.where(id: [er1, er2])).to be_empty end - it "destroys all distributed_orders upon destroy" do + it "raises a DeleteRestrictionError on destroy if distributed_orders exist" do enterprise = create(:distributor_enterprise) - order_ids = create_list(:order, 2, distributor: enterprise).map(&:id) + create_list(:order, 2, distributor: enterprise) - expect(Spree::Order.where(id: order_ids)).to exist - enterprise.destroy - expect(Spree::Order.where(id: order_ids)).not_to exist + expect do + enterprise.destroy + end.to raise_error(ActiveRecord::DeleteRestrictionError, + /Cannot delete record because of dependent distributed_orders/) + .and change { Spree::Order.count }.by(0) end - it "destroys all distributor_payment_methods upon destroy" do + it "raises an DeleteRestrictionError on destroy if distributor_payment_methods exist" do enterprise = create(:distributor_enterprise) - payment_method_ids = create_list(:distributor_payment_method, 2, - distributor: enterprise).map(&:id) + create_list(:distributor_payment_method, 2, distributor: enterprise) - expect(DistributorPaymentMethod.where(id: payment_method_ids)).to exist - enterprise.destroy - expect(DistributorPaymentMethod.where(id: payment_method_ids)).not_to exist + expect do + enterprise.destroy + end.to raise_error(ActiveRecord::DeleteRestrictionError, + /Cannot delete record because of dependent distributor_payment_methods/) + .and change { DistributorPaymentMethod.count }.by(0) end - it "destroys all distributor_shipping_methods upon destroy" do - enterprise = create(:enterprise) - shipping_method_ids = create_list(:distributor_shipping_method, 2, - distributor: enterprise).map(&:id) + it "raises an DeleteRestrictionError on destroy if distributor_shipping_methods exist" do + enterprise = create(:distributor_enterprise) + create_list(:distributor_shipping_method, 2, distributor: enterprise) - expect(DistributorShippingMethod.where(id: shipping_method_ids)).to exist - enterprise.destroy - expect(DistributorShippingMethod.where(id: shipping_method_ids)).not_to exist + expect do + enterprise.destroy + end.to raise_error(ActiveRecord::DeleteRestrictionError, + /Cannot delete record because of dependent distributor_shipping_methods/) + .and change { DistributorShippingMethod.count }.by(0) end - it "destroys all enterprise_fees upon destroy" do + it "does not destroy enterprise_fees upon destroy" do enterprise = create(:enterprise) - fee_ids = create_list(:enterprise_fee, 2, enterprise:).map(&:id) + create_list(:enterprise_fee, 2, enterprise:) - expect(EnterpriseFee.where(id: fee_ids)).to exist - enterprise.destroy - expect(EnterpriseFee.where(id: fee_ids)).not_to exist + expect do + enterprise.destroy + end.to raise_error(ActiveRecord::DeleteRestrictionError, + /Cannot delete record because of dependent enterprise_fees/) + .and change { EnterpriseFee.count }.by(0) end - it "destroys all vouchers upon destroy" do + it "does not destroy vouchers upon destroy" do enterprise = create(:enterprise) - voucher_ids = (1..2).map do |code| + (1..2).map do |code| create(:voucher, enterprise:, code: "new code #{code}") - end.map(&:id) + end - expect(Voucher.where(id: voucher_ids)).to exist - enterprise.destroy - expect(Voucher.where(id: voucher_ids)).not_to exist + expect do + enterprise.destroy + end.to raise_error(ActiveRecord::DeleteRestrictionError, + /Cannot delete record because of dependent vouchers/) + .and change { Voucher.count }.by(0) end describe "relationships to other enterprises" do