diff --git a/lib/open_food_web/distributor_change_validator.rb b/lib/open_food_web/distributor_change_validator.rb index 2d4a0e7a87..cd7ed4bf2f 100644 --- a/lib/open_food_web/distributor_change_validator.rb +++ b/lib/open_food_web/distributor_change_validator.rb @@ -3,17 +3,17 @@ class DistributorChangeValidator def initialize order @order = order end - + def can_change_distributor? # Distributor may not be changed once an item has been added to the cart/order - @order.line_items.empty? + @order.line_items.empty? || available_distributors(Enterprise.all).length > 1 end 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? || (available_distributors(Enterprise.all) || []).include?(distributor) end - + def available_distributors enterprises enterprises.select do |e| (@order.line_item_variants - e.distributed_variants).empty? 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 aac061cd9e..9b6b848483 100644 --- a/spec/lib/open_food_web/distributor_change_validator_spec.rb +++ b/spec/lib/open_food_web/distributor_change_validator_spec.rb @@ -10,8 +10,15 @@ describe DistributorChangeValidator do subject.can_change_distributor?.should be_true end - it "does not allow distributor to be changed if line_items is not empty" do + it "allows distributor to be changed if there are multiple available distributors" do order.stub(:line_items) { [1, 2, 3] } + subject.stub(:available_distributors).and_return([1, 2]) + subject.can_change_distributor?.should be_true + end + + it "does not allow distributor to be changed if there are no other available distributors" do + order.stub(:line_items) { [1, 2, 3] } + subject.stub(:available_distributors).and_return([1]) subject.can_change_distributor?.should be_false end end