Fix Rubocop Rails issue: Rails/HasManyOrHasOneDependent

This commit is contained in:
Anthony Musyoki
2024-03-21 15:02:38 +03:00
parent 8d166ed3e1
commit 5559816e12
4 changed files with 44 additions and 3 deletions

View File

@@ -43,7 +43,9 @@ class Enterprise < ApplicationRecord
foreign_key: 'supplier_id',
dependent: :destroy
has_many :supplied_variants, through: :supplied_products, source: :variants
has_many :distributed_orders, class_name: 'Spree::Order', foreign_key: 'distributor_id'
has_many :distributed_orders, class_name: 'Spree::Order',
foreign_key: 'distributor_id',
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
@@ -52,9 +54,9 @@ class Enterprise < ApplicationRecord
belongs_to :owner, class_name: 'Spree::User',
inverse_of: :owned_enterprises
has_many :distributor_payment_methods,
inverse_of: :distributor, foreign_key: :distributor_id
inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy
has_many :distributor_shipping_methods,
inverse_of: :distributor, foreign_key: :distributor_id
inverse_of: :distributor, foreign_key: :distributor_id, dependent: :destroy
has_many :payment_methods, through: :distributor_payment_methods
has_many :shipping_methods, through: :distributor_shipping_methods
has_many :customers, dependent: :destroy

View File

@@ -24,4 +24,9 @@ FactoryBot.define do
distributors { [FactoryBot.create(:stripe_account).enterprise] }
preferred_enterprise_id { distributors.first.id }
end
factory :distributor_payment_method, class: DistributorPaymentMethod do
distributor { FactoryBot.create(:distributor_enterprise) }
payment_method { FactoryBot.create(:payment_method) }
end
end

View File

@@ -68,4 +68,9 @@ FactoryBot.define do
distributors { [create(:distributor_enterprise_with_tax)] }
end
end
factory :distributor_shipping_method, class: DistributorShippingMethod do
shipping_method { FactoryBot.create(:shipping_method) }
distributor { FactoryBot.create(:distributor_enterprise) }
end
end

View File

@@ -57,6 +57,35 @@ describe Enterprise do
expect(EnterpriseRelationship.where(id: [er1, er2])).to be_empty
end
it "destroys all distributed_orders upon destroy" do
enterprise = create(:distributor_enterprise)
order_ids = create_list(:order, 2, distributor: enterprise).map(&:id)
expect(Spree::Order.where(id: order_ids)).to exist
enterprise.destroy
expect(Spree::Order.where(id: order_ids)).not_to exist
end
it "destroys all distributor_payment_methods upon destroy" do
enterprise = create(:distributor_enterprise)
payment_method_ids = create_list(:distributor_payment_method, 2,
distributor: enterprise).map(&:id)
expect(DistributorPaymentMethod.where(id: payment_method_ids)).to exist
enterprise.destroy
expect(DistributorPaymentMethod.where(id: payment_method_ids)).not_to exist
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)
expect(DistributorShippingMethod.where(id: shipping_method_ids)).to exist
enterprise.destroy
expect(DistributorShippingMethod.where(id: shipping_method_ids)).not_to exist
end
describe "relationships to other enterprises" do
let(:e) { create(:distributor_enterprise) }
let(:p) { create(:supplier_enterprise) }