From 4e5f0b9963d03536d9e0a7ed66e61580b40270b5 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 14 Jun 2013 14:57:08 +1000 Subject: [PATCH] Set potential distributor and order cycle before attempting add to cart, revert on failure --- app/models/spree/order_populator_decorator.rb | 17 +++++++++++++++-- spec/models/order_populator_spec.rb | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/app/models/spree/order_populator_decorator.rb b/app/models/spree/order_populator_decorator.rb index c1581c351d..953492297b 100644 --- a/app/models/spree/order_populator_decorator.rb +++ b/app/models/spree/order_populator_decorator.rb @@ -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 diff --git a/spec/models/order_populator_spec.rb b/spec/models/order_populator_spec.rb index e63df2e891..a2ae2b5328 100644 --- a/spec/models/order_populator_spec.rb +++ b/spec/models/order_populator_spec.rb @@ -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