Fix Rubocop: Hard delete paranoid associations

As much as the associated models act_as_paranoid, it
doesnt make sense to keep them around after deleting the enterprise
This commit is contained in:
Anthony Musyoki
2024-03-26 10:57:40 +03:00
parent 645cb10864
commit c2cbe4f0bf
2 changed files with 33 additions and 2 deletions

View File

@@ -48,7 +48,7 @@ class Enterprise < ApplicationRecord
dependent: :destroy
belongs_to :address, class_name: 'Spree::Address'
belongs_to :business_address, optional: true, class_name: 'Spree::Address', dependent: :destroy
has_many :enterprise_fees
has_many :enterprise_fees, dependent: nil # paranoid association is deleted in a before_destroy
has_many :enterprise_roles, dependent: :destroy
has_many :users, through: :enterprise_roles
belongs_to :owner, class_name: 'Spree::User',
@@ -63,7 +63,7 @@ class Enterprise < ApplicationRecord
has_many :inventory_items, dependent: :destroy
has_many :tag_rules, dependent: :destroy
has_one :stripe_account, dependent: :destroy
has_many :vouchers
has_many :vouchers, dependent: nil # paranoid association is deleted in a before_destroy
has_many :connected_apps, dependent: :destroy
has_one :custom_tab, dependent: :destroy
@@ -130,6 +130,9 @@ 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
after_create_commit :send_welcome_email
@@ -587,4 +590,12 @@ 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

View File

@@ -86,6 +86,26 @@ describe Enterprise do
expect(DistributorShippingMethod.where(id: shipping_method_ids)).not_to exist
end
it "destroys all enterprise_fees upon destroy" do
enterprise = create(:enterprise)
fee_ids = create_list(:enterprise_fee, 2, enterprise:).map(&:id)
expect(EnterpriseFee.where(id: fee_ids)).to exist
enterprise.destroy
expect(EnterpriseFee.where(id: fee_ids)).not_to exist
end
it "destroys all vouchers upon destroy" do
enterprise = create(:enterprise)
voucher_ids = (1..2).map do |code|
create(:voucher, enterprise:, code: "new code #{code}")
end.map(&:id)
expect(Voucher.where(id: voucher_ids)).to exist
enterprise.destroy
expect(Voucher.where(id: voucher_ids)).not_to exist
end
describe "relationships to other enterprises" do
let(:e) { create(:distributor_enterprise) }
let(:p) { create(:supplier_enterprise) }