Fix Enterprise

This commit is contained in:
Gaetan Craig-Riou
2024-02-27 16:32:36 +11:00
parent d073a181e9
commit 50bd274715
2 changed files with 24 additions and 25 deletions

View File

@@ -39,13 +39,13 @@ class Enterprise < ApplicationRecord
class_name: 'EnterpriseGroup'
has_many :producer_properties, foreign_key: 'producer_id', dependent: :destroy
has_many :properties, through: :producer_properties
has_many :supplied_products, class_name: 'Spree::Product',
foreign_key: 'supplier_id',
dependent: :destroy
has_many :supplied_variants, through: :supplied_products, source: :variants
has_many :supplied_variants,
class_name: 'Spree::Variant', foreign_key: 'supplier_id', dependent: :destroy
has_many :supplied_products, through: :supplied_variants, source: :product
has_many :distributed_orders, class_name: 'Spree::Order',
foreign_key: 'distributor_id',
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: :restrict_with_exception
@@ -167,7 +167,7 @@ class Enterprise < ApplicationRecord
scope :is_distributor, -> { where.not(sells: 'none') }
scope :is_hub, -> { where(sells: 'any') }
scope :supplying_variant_in, lambda { |variants|
joins(supplied_products: :variants).
joins(:supplied_variants).
where(spree_variants: { id: variants }).
select('DISTINCT enterprises.*')
}

View File

@@ -37,13 +37,13 @@ RSpec.describe Enterprise do
expect(EnterpriseRole.where(id: role.id)).to be_empty
end
xit "destroys supplied products upon destroy" do
s = create(:supplier_enterprise)
p = create(:simple_product, supplier: s)
it "destroys supplied variants upon destroy" do
supplier = create(:supplier_enterprise)
variant = create(:variant, supplier:)
s.destroy
variant.destroy
expect(Spree::Product.where(id: p.id)).to be_empty
expect(Spree::Variant.where(id: variant.id)).to be_empty
end
it "destroys relationships upon destroy" do
@@ -567,31 +567,30 @@ RSpec.describe Enterprise do
end
end
describe "supplying_variant_in" do
describe ".supplying_variant_in" do
it "finds producers by supply of variant" do
s = create(:supplier_enterprise)
p = create(:simple_product, supplier: s)
v = create(:variant, product: p)
supplier = create(:supplier_enterprise)
variant = create(:variant, supplier:)
expect(Enterprise.supplying_variant_in([v])).to eq([s])
expect(Enterprise.supplying_variant_in([variant])).to eq([supplier])
end
it "returns multiple enterprises when given multiple variants" do
s1 = create(:supplier_enterprise)
s2 = create(:supplier_enterprise)
p1 = create(:simple_product, supplier: s1)
p2 = create(:simple_product, supplier: s2)
supplier1 = create(:supplier_enterprise)
supplier2 = create(:supplier_enterprise)
variant1 = create(:variant, supplier: supplier1)
variant2 = create(:variant, supplier: supplier2)
expect(Enterprise.supplying_variant_in([p1.variants.first,
p2.variants.first])).to match_array [s1, s2]
expect(Enterprise.supplying_variant_in([variant1, variant2]))
.to match_array([supplier1, supplier2])
end
it "does not return duplicates" do
s = create(:supplier_enterprise)
p1 = create(:simple_product, supplier: s)
p2 = create(:simple_product, supplier: s)
supplier = create(:supplier_enterprise)
variant1 = create(:variant, supplier:)
variant2 = create(:variant, supplier:)
expect(Enterprise.supplying_variant_in([p1.variants.first, p2.variants.first])).to eq([s])
expect(Enterprise.supplying_variant_in([variant1, variant2])).to eq([supplier])
end
end