mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-04 22:16:08 +00:00
Replace Enterprise.distributing_product and Enterprise.distributing_any_product_of with Enterprise.distributing_products
This commit is contained in:
@@ -162,12 +162,7 @@ class Enterprise < ActiveRecord::Base
|
||||
select('DISTINCT enterprises.*')
|
||||
}
|
||||
|
||||
scope :distributing_product, lambda { |product|
|
||||
with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer.
|
||||
where('product_distributions.product_id = ? OR spree_variants.product_id = ?', product, product).
|
||||
select('DISTINCT enterprises.*')
|
||||
}
|
||||
scope :distributing_any_product_of, lambda { |products|
|
||||
scope :distributing_products, lambda { |products|
|
||||
with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer.
|
||||
where('product_distributions.product_id IN (?) OR spree_variants.product_id IN (?)', products, products).
|
||||
select('DISTINCT enterprises.*')
|
||||
@@ -472,6 +467,6 @@ class Enterprise < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def touch_distributors
|
||||
Enterprise.distributing_product(self.supplied_products).each(&:touch)
|
||||
Enterprise.distributing_products(self.supplied_products).each(&:touch)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -215,7 +215,7 @@ Spree::Product.class_eval do
|
||||
end
|
||||
|
||||
def touch_distributors
|
||||
Enterprise.distributing_product(self).each(&:touch)
|
||||
Enterprise.distributing_products(self).each(&:touch)
|
||||
end
|
||||
|
||||
def add_primary_taxon_to_taxons
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
%tbody
|
||||
- order = current_order(false)
|
||||
- validator = DistributionChangeValidator.new(order)
|
||||
- Enterprise.distributing_product(@product).each do |distributor|
|
||||
- Enterprise.distributing_products(@product).each do |distributor|
|
||||
- if !order.nil? && distributor == order.distributor
|
||||
%tr.odd
|
||||
%td
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class DistributionChangeValidator
|
||||
|
||||
|
||||
def initialize order
|
||||
@order = order
|
||||
end
|
||||
@@ -29,7 +29,7 @@ class DistributionChangeValidator
|
||||
end
|
||||
|
||||
def available_distributors_for(product)
|
||||
distributors = Enterprise.distributing_product(product)
|
||||
distributors = Enterprise.distributing_products(product)
|
||||
|
||||
if @order.andand.line_items.present?
|
||||
distributors = available_distributors(distributors)
|
||||
|
||||
@@ -87,7 +87,7 @@ describe DistributionChangeValidator do
|
||||
enterprise_with_some_variants.stub(:distributed_variants) { [variant1, variant3] } # Only some variants
|
||||
enterprise_with_some_plus_extras = double(:enterprise)
|
||||
enterprise_with_some_plus_extras.stub(:distributed_variants) { [variant1, variant2, variant3, variant4] } # Only some variants, plus extras
|
||||
|
||||
|
||||
subject.available_distributors([enterprise_with_some_variants]).should_not include enterprise_with_some_variants
|
||||
subject.available_distributors([enterprise_with_some_plus_extras]).should_not include enterprise_with_some_plus_extras
|
||||
end
|
||||
@@ -97,10 +97,10 @@ describe DistributionChangeValidator do
|
||||
order.stub(:line_item_variants) { line_item_variants }
|
||||
enterprise = double(:enterprise)
|
||||
enterprise.stub(:distributed_variants) { [variant1, variant2, variant3, variant4, variant5] } # Excess variants
|
||||
|
||||
|
||||
subject.available_distributors([enterprise]).should == [enterprise]
|
||||
end
|
||||
|
||||
|
||||
it "matches no enterprises when none are provided" do
|
||||
subject.available_distributors([]).should == []
|
||||
end
|
||||
@@ -201,7 +201,7 @@ describe DistributionChangeValidator do
|
||||
describe "finding available distributors for a product" do
|
||||
it "returns enterprises distributing the product when there's no order" do
|
||||
subject = DistributionChangeValidator.new(nil)
|
||||
Enterprise.stub(:distributing_product).and_return([1, 2, 3])
|
||||
Enterprise.stub(:distributing_products).and_return([1, 2, 3])
|
||||
subject.should_receive(:available_distributors).never
|
||||
|
||||
subject.available_distributors_for(product).should == [1, 2, 3]
|
||||
@@ -209,7 +209,7 @@ describe DistributionChangeValidator do
|
||||
|
||||
it "returns enterprises distributing the product when there's no order items" do
|
||||
order.stub(:line_items) { [] }
|
||||
Enterprise.stub(:distributing_product).and_return([1, 2, 3])
|
||||
Enterprise.stub(:distributing_products).and_return([1, 2, 3])
|
||||
subject.should_receive(:available_distributors).never
|
||||
|
||||
subject.available_distributors_for(product).should == [1, 2, 3]
|
||||
@@ -217,7 +217,7 @@ describe DistributionChangeValidator do
|
||||
|
||||
it "filters by available distributors when there are order items" do
|
||||
order.stub(:line_items) { [1, 2, 3] }
|
||||
Enterprise.stub(:distributing_product).and_return([1, 2, 3])
|
||||
Enterprise.stub(:distributing_products).and_return([1, 2, 3])
|
||||
subject.should_receive(:available_distributors).and_return([2])
|
||||
|
||||
subject.available_distributors_for(product).should == [2]
|
||||
|
||||
@@ -542,40 +542,38 @@ describe Enterprise do
|
||||
end
|
||||
end
|
||||
|
||||
describe "distributing_product" do
|
||||
describe "distributing_products" do
|
||||
it "returns enterprises distributing via a product distribution" do
|
||||
d = create(:distributor_enterprise)
|
||||
p = create(:product, distributors: [d])
|
||||
Enterprise.distributing_product(p).should == [d]
|
||||
Enterprise.distributing_products(p).should == [d]
|
||||
end
|
||||
|
||||
it "returns enterprises distributing via an order cycle" do
|
||||
d = create(:distributor_enterprise)
|
||||
p = create(:product)
|
||||
oc = create(:simple_order_cycle, distributors: [d], variants: [p.master])
|
||||
Enterprise.distributing_product(p).should == [d]
|
||||
Enterprise.distributing_products(p).should == [d]
|
||||
end
|
||||
end
|
||||
|
||||
describe "distributing_any_product_of" do
|
||||
it "returns enterprises distributing via a product distribution" do
|
||||
d = create(:distributor_enterprise)
|
||||
p = create(:product, distributors: [d])
|
||||
Enterprise.distributing_any_product_of([p]).should == [d]
|
||||
Enterprise.distributing_products([p]).should == [d]
|
||||
end
|
||||
|
||||
it "returns enterprises distributing via an order cycle" do
|
||||
d = create(:distributor_enterprise)
|
||||
p = create(:product)
|
||||
oc = create(:simple_order_cycle, distributors: [d], variants: [p.master])
|
||||
Enterprise.distributing_any_product_of([p]).should == [d]
|
||||
Enterprise.distributing_products([p]).should == [d]
|
||||
end
|
||||
|
||||
it "does not return duplicate enterprises" do
|
||||
d = create(:distributor_enterprise)
|
||||
p1 = create(:product, distributors: [d])
|
||||
p2 = create(:product, distributors: [d])
|
||||
Enterprise.distributing_any_product_of([p1, p2]).should == [d]
|
||||
Enterprise.distributing_products([p1, p2]).should == [d]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user