mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Build list of variants removed from the cart when overwriting
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user