Allow changing distributor when there are alternate distributors available that can service the cart's order

This commit is contained in:
Rohan Mitchell
2013-05-27 13:11:30 +10:00
parent e3a419993c
commit a21783c814
2 changed files with 11 additions and 4 deletions

View File

@@ -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?

View File

@@ -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