mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Set potential distributor and order cycle before attempting add to cart, revert on failure
This commit is contained in:
@@ -6,11 +6,19 @@ Spree::OrderPopulator.class_eval do
|
||||
errors.add(:base, "That distributor can't supply all the products in your cart. Please choose another.")
|
||||
end
|
||||
|
||||
populate_without_distribution_validation(from_hash) if valid?
|
||||
|
||||
# Set order distributor and order cycle
|
||||
@orig_distributor, @orig_order_cycle = orig_distributor_and_order_cycle
|
||||
cart_distribution_set = false
|
||||
if valid?
|
||||
set_cart_distributor_and_order_cycle @distributor, @order_cycle
|
||||
cart_distribution_set = true
|
||||
end
|
||||
|
||||
populate_without_distribution_validation(from_hash) if valid?
|
||||
|
||||
# Undo distribution setting if validation falied when adding a product
|
||||
if !valid? && cart_distribution_set
|
||||
set_cart_distributor_and_order_cycle @orig_distributor, @orig_order_cycle
|
||||
end
|
||||
|
||||
valid?
|
||||
@@ -35,6 +43,11 @@ Spree::OrderPopulator.class_eval do
|
||||
|
||||
private
|
||||
|
||||
def orig_distributor_and_order_cycle
|
||||
[@order.distributor, @order.order_cycle]
|
||||
end
|
||||
|
||||
|
||||
def load_distributor_and_order_cycle(from_hash)
|
||||
distributor = from_hash[:distributor_id].present? ?
|
||||
Enterprise.is_distributor.find(from_hash[:distributor_id]) : nil
|
||||
|
||||
@@ -7,6 +7,8 @@ module Spree
|
||||
let(:params) { double(:params) }
|
||||
let(:distributor) { double(:distributor) }
|
||||
let(:order_cycle) { double(:order_cycle) }
|
||||
let(:orig_distributor) { double(:distributor) }
|
||||
let(:orig_order_cycle) { double(:order_cycle) }
|
||||
let(:op) { OrderPopulator.new(order, currency) }
|
||||
|
||||
describe "populate" do
|
||||
@@ -16,6 +18,8 @@ module Spree
|
||||
and_return([distributor, order_cycle])
|
||||
op.should_receive(:distributor_can_supply_products_in_cart).with(distributor).
|
||||
and_return(false)
|
||||
op.stub(:orig_distributor_and_order_cycle).and_return([orig_distributor,
|
||||
orig_order_cycle])
|
||||
op.should_receive(:populate_without_distribution_validation).never
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).never
|
||||
|
||||
@@ -23,11 +27,13 @@ module Spree
|
||||
op.errors.to_a.should == ["That distributor can't supply all the products in your cart. Please choose another."]
|
||||
end
|
||||
|
||||
it "doesn't set cart distributor and order cycle if populate fails" do
|
||||
it "resets cart distributor and order cycle if populate fails" do
|
||||
op.should_receive(:load_distributor_and_order_cycle).with(params).
|
||||
and_return([distributor, order_cycle])
|
||||
op.should_receive(:distributor_can_supply_products_in_cart).with(distributor).
|
||||
and_return(true)
|
||||
op.stub(:orig_distributor_and_order_cycle).and_return([orig_distributor,
|
||||
orig_order_cycle])
|
||||
|
||||
op.class_eval do
|
||||
def populate_without_distribution_validation(from_hash)
|
||||
@@ -35,7 +41,8 @@ module Spree
|
||||
end
|
||||
end
|
||||
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).never
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).with(distributor, order_cycle)
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).with(orig_distributor, orig_order_cycle)
|
||||
|
||||
op.populate(params).should be_false
|
||||
op.errors.to_a.should == ["Something went wrong."]
|
||||
@@ -46,6 +53,8 @@ module Spree
|
||||
and_return([distributor, order_cycle])
|
||||
op.should_receive(:distributor_can_supply_products_in_cart).with(distributor).
|
||||
and_return(true)
|
||||
op.stub(:orig_distributor_and_order_cycle).and_return([orig_distributor,
|
||||
orig_order_cycle])
|
||||
op.should_receive(:populate_without_distribution_validation).with(params)
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).with(distributor, order_cycle)
|
||||
|
||||
@@ -139,6 +148,12 @@ module Spree
|
||||
end
|
||||
end
|
||||
|
||||
it "provides the original distributor and order cycle for the order" do
|
||||
order.should_receive(:distributor).and_return(orig_distributor)
|
||||
order.should_receive(:order_cycle).and_return(orig_order_cycle)
|
||||
op.send(:orig_distributor_and_order_cycle).should == [orig_distributor,
|
||||
orig_order_cycle]
|
||||
end
|
||||
end
|
||||
|
||||
describe "validations" do
|
||||
|
||||
Reference in New Issue
Block a user