mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Allow changing distributor when there are alternate distributors available that can service the cart's order
This commit is contained in:
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user