From c432ed9e08b845dc39159a00126e481f4e651c53 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 6 Nov 2015 10:19:25 +1100 Subject: [PATCH] Build list of variants removed from the cart when overwriting --- app/models/spree/order_populator_decorator.rb | 10 ++++++++ spec/models/spree/order_populator_spec.rb | 23 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/app/models/spree/order_populator_decorator.rb b/app/models/spree/order_populator_decorator.rb index d9c261593e..32d6afe12d 100644 --- a/app/models/spree/order_populator_decorator.rb +++ b/app/models/spree/order_populator_decorator.rb @@ -75,6 +75,12 @@ Spree::OrderPopulator.class_eval do li_added || li_quantity_changed || li_max_quantity_changed end + def variants_removed(variants_data) + variant_ids_given = variants_data.map { |data| data[:variant_id] } + + (variant_ids_in_cart - variant_ids_given).uniq + end + def check_order_cycle_provided_for(variant) order_cycle_provided = (!order_cycle_required_for(variant) || @order_cycle.present?) errors.add(:base, "Please choose an order cycle for this order.") unless order_cycle_provided @@ -97,4 +103,8 @@ Spree::OrderPopulator.class_eval do def line_item_for_variant_id(variant_id) order.find_line_item_by_variant Spree::Variant.find(variant_id) end + + def variant_ids_in_cart + @order.line_items.map &:variant_id + end end diff --git a/spec/models/spree/order_populator_spec.rb b/spec/models/spree/order_populator_spec.rb index 99b980e20d..1e6e06677d 100644 --- a/spec/models/spree/order_populator_spec.rb +++ b/spec/models/spree/order_populator_spec.rb @@ -46,7 +46,6 @@ module Spree end describe "varies_from_cart" do - #let(:order) { create(:order) } let(:variant) { double(:variant, id: 123) } it "returns true when item is not in cart and a quantity is specified" do @@ -93,6 +92,28 @@ module Spree end end + describe "variants_removed" do + it "returns the variant ids when one is in the cart but not in those given" do + op.stub(:variant_ids_in_cart) { [123] } + op.send(:variants_removed, []).should == [123] + end + + it "returns nothing when all items in the cart are provided" do + op.stub(:variant_ids_in_cart) { [123] } + op.send(:variants_removed, [{variant_id: 123}]).should == [] + end + + it "returns nothing when items are added to cart" do + op.stub(:variant_ids_in_cart) { [123] } + op.send(:variants_removed, [{variant_id: 123}, {variant_id: 456}]).should == [] + end + + it "does not return duplicates" do + op.stub(:variant_ids_in_cart) { [123, 123] } + op.send(:variants_removed, []).should == [123] + end + end + describe "attempt_cart_add" do it "performs additional validations" do variant = double(:variant)