diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 52c51335c8..c90b46af63 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -41,7 +41,7 @@ class Enterprise < ActiveRecord::Base "#{id}-#{name.parameterize}" end - def available_variants + def distributed_variants Spree::Product.in_distributor(self).map { |product| product.variants + [product.master] }.flatten end diff --git a/lib/open_food_web/distributor_change_validator.rb b/lib/open_food_web/distributor_change_validator.rb index ca7545eefc..2d4a0e7a87 100644 --- a/lib/open_food_web/distributor_change_validator.rb +++ b/lib/open_food_web/distributor_change_validator.rb @@ -16,7 +16,7 @@ class DistributorChangeValidator def available_distributors enterprises enterprises.select do |e| - (@order.line_item_variants - e.available_variants).empty? + (@order.line_item_variants - e.distributed_variants).empty? end end -end \ No newline at end of file +end diff --git a/spec/lib/open_food_web/distributor_change_validator_spec.rb b/spec/lib/open_food_web/distributor_change_validator_spec.rb index d8f8967000..aac061cd9e 100644 --- a/spec/lib/open_food_web/distributor_change_validator_spec.rb +++ b/spec/lib/open_food_web/distributor_change_validator_spec.rb @@ -25,7 +25,7 @@ describe DistributorChangeValidator do line_item_variants = [variant1, variant3, variant5] order.stub(:line_item_variants){ line_item_variants } enterprise = double(:enterprise) - enterprise.stub(:available_variants){ line_item_variants } # Exactly the same variants as the order + enterprise.stub(:distributed_variants){ line_item_variants } # Exactly the same variants as the order subject.available_distributors([enterprise]).should == [enterprise] end @@ -37,7 +37,7 @@ describe DistributorChangeValidator do line_item_variants = [variant1, variant3, variant5] order.stub(:line_item_variants){ line_item_variants } enterprise = double(:enterprise) - enterprise.stub(:available_variants){ [] } # No variants + enterprise.stub(:distributed_variants){ [] } # No variants subject.available_distributors([enterprise]).should_not include enterprise end @@ -51,9 +51,9 @@ describe DistributorChangeValidator do line_item_variants = [variant1, variant3, variant5] order.stub(:line_item_variants){ line_item_variants } enterprise_with_some_variants = double(:enterprise) - enterprise_with_some_variants.stub(:available_variants){ [variant1, variant3] } # Only some variants + 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(:available_variants){ [variant1, variant2, variant3, variant4] } # Only some variants, plus extras + 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 @@ -68,7 +68,7 @@ describe DistributorChangeValidator do line_item_variants = [variant1, variant3, variant5] order.stub(:line_item_variants){ line_item_variants } enterprise = double(:enterprise) - enterprise.stub(:available_variants){ [variant1, variant2, variant3, variant4, variant5] } # Excess variants + enterprise.stub(:distributed_variants){ [variant1, variant2, variant3, variant4, variant5] } # Excess variants subject.available_distributors([enterprise]).should == [enterprise] end @@ -77,4 +77,4 @@ describe DistributorChangeValidator do subject.available_distributors([]).should == [] end end -end \ No newline at end of file +end diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index b1c484a789..7b4548b431 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -94,26 +94,25 @@ describe Enterprise do end end - # TODO: Rename to distributed_variants? describe "finding variants distributed by the enterprise" do it "finds the master variant" do d = create(:distributor_enterprise) p = create(:product, distributors: [d]) - d.available_variants.should == [p.master] + d.distributed_variants.should == [p.master] end it "finds other variants" do d = create(:distributor_enterprise) p = create(:product, distributors: [d]) v = create(:variant, product: p) - d.available_variants.sort.should == [p.master, v].sort + d.distributed_variants.sort.should == [p.master, v].sort end it "finds variants distributed by order cycle" do d = create(:distributor_enterprise) p = create(:product) oc = create(:simple_order_cycle, distributors: [d], variants: [p.master]) - d.available_variants.should == [p.master] + d.distributed_variants.should == [p.master] end end end