Do not show duplicate products with in_supplier_or_distributor scope

This commit is contained in:
Rohan Mitchell
2012-11-10 12:26:41 +11:00
parent 9a9bef5304
commit 580486a347
2 changed files with 14 additions and 1 deletions

View File

@@ -12,7 +12,8 @@ Spree::Product.class_eval do
scope :in_supplier, lambda { |supplier| where(:supplier_id => supplier) }
scope :in_distributor, lambda { |distributor| joins(:product_distributions).where('product_distributions.distributor_id = ?', (distributor.respond_to?(:id) ? distributor.id : distributor.to_i)) }
scope :in_supplier_or_distributor, lambda { |enterprise| joins('LEFT OUTER JOIN product_distributions ON product_distributions.product_id=spree_products.id').
scope :in_supplier_or_distributor, lambda { |enterprise| select('distinct spree_products.*').
joins('LEFT OUTER JOIN product_distributions ON product_distributions.product_id=spree_products.id').
where('supplier_id=? OR product_distributions.distributor_id=?',
enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i,
enterprise.respond_to?(:id) ? enterprise.id : enterprise.to_i) }

View File

@@ -47,6 +47,18 @@ describe Spree::Product do
Spree::Product.in_supplier_or_distributor(s).should == [p]
Spree::Product.in_supplier_or_distributor(d).should == [p]
end
it "shows each product once when it is distributed by many distributors" do
s = create(:supplier_enterprise)
d1 = create(:distributor_enterprise)
d2 = create(:distributor_enterprise)
d3 = create(:distributor_enterprise)
p = create(:product, :supplier => s, :distributors => [d1, d2, d3])
[s, d1, d2, d3].each do |enterprise|
Spree::Product.in_supplier_or_distributor(enterprise).should == [p]
end
end
end
end