From be29974dd8b999bb7301c842e8188c2e6894abce Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 17 Jun 2013 15:00:19 +1000 Subject: [PATCH] DistributionChangeValidator checks if an order can change to a specified new distributION --- .../distribution_change_validator.rb | 10 ++++- .../distribution_change_validator_spec.rb | 43 ++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/open_food_web/distribution_change_validator.rb b/lib/open_food_web/distribution_change_validator.rb index 0d1062aca4..58955d057f 100644 --- a/lib/open_food_web/distribution_change_validator.rb +++ b/lib/open_food_web/distribution_change_validator.rb @@ -4,11 +4,19 @@ class DistributionChangeValidator @order = order end - def can_change_to_distributor? distributor + def can_change_to_distributor?(distributor) # Distributor may not be changed once an item has been added to the cart/order, unless all items are available from the specified distributor @order.line_items.empty? || all_available_distributors.include?(distributor) end + def can_change_to_distribution?(distributor, order_cycle) + (@order.line_item_variants - variants_available_for_distribution(distributor, order_cycle)).empty? + end + + def variants_available_for_distribution(distributor, order_cycle) + distributor.distributed_variants + order_cycle.distributed_variants_by(distributor) + end + def distributor_available_for?(product) @order.nil? || available_distributors_for(product).present? end diff --git a/spec/lib/open_food_web/distribution_change_validator_spec.rb b/spec/lib/open_food_web/distribution_change_validator_spec.rb index 1fa6745b75..dc70f0c833 100644 --- a/spec/lib/open_food_web/distribution_change_validator_spec.rb +++ b/spec/lib/open_food_web/distribution_change_validator_spec.rb @@ -5,7 +5,46 @@ describe DistributionChangeValidator do let(:subject) { DistributionChangeValidator.new(order) } let(:product) { double(:product) } - context "finding distributors which have the same variants" do + describe "checking if an order can change to a specified new distribution" do + let(:distributor) { double(:distributor) } + let(:order_cycle) { double(:order_cycle) } + + it "returns false when a variant is not available for the specified distribution" do + order.should_receive(:line_item_variants) { [1] } + subject.should_receive(:variants_available_for_distribution). + with(distributor, order_cycle) { [] } + subject.can_change_to_distribution?(distributor, order_cycle).should be_false + end + + it "returns true when all variants are available for the specified distribution" do + order.should_receive(:line_item_variants) { [1] } + subject.should_receive(:variants_available_for_distribution). + with(distributor, order_cycle) { [1] } + subject.can_change_to_distribution?(distributor, order_cycle).should be_true + end + end + + describe "finding variants that are available through a particular distribution" do + it "finds variants distributed by product distribution" do + v = double(:variant) + d = double(:distributor, distributed_variants: [v]) + oc = double(:order_cycle, distributed_variants_by: []) + + subject.variants_available_for_distribution(d, oc).should == [v] + end + + it "finds variants distributed by order cycle" do + v = double(:variant) + d = double(:distributor, distributed_variants: []) + oc = double(:order_cycle) + + oc.should_receive(:distributed_variants_by).with(d) { [v] } + + subject.variants_available_for_distribution(d, oc).should == [v] + end + end + + describe "finding distributors which have the same variants" do let(:variant1) { double(:variant) } let(:variant2) { double(:variant) } let(:variant3) { double(:variant) } @@ -56,7 +95,7 @@ describe DistributionChangeValidator do end end - context "finding order cycles which have the same variants" do + describe "finding order cycles which have the same variants" do let(:variant1) { double(:variant) } let(:variant2) { double(:variant) } let(:variant3) { double(:variant) }