mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-10 03:30:22 +00:00
Add method to check whether a passed-in cart value varies from the cart
This commit is contained in:
@@ -65,6 +65,16 @@ Spree::OrderPopulator.class_eval do
|
||||
DistributionChangeValidator.new(@order).can_change_to_distribution?(distributor, order_cycle)
|
||||
end
|
||||
|
||||
def varies_from_cart(variant_data)
|
||||
li = line_item_for_variant_id variant_data[:variant_id]
|
||||
|
||||
li_added = li.nil? && (variant_data[:quantity].to_i > 0 || variant_data[:max_quantity].to_i > 0)
|
||||
li_quantity_changed = li.present? && li.quantity.to_i != variant_data[:quantity].to_i
|
||||
li_max_quantity_changed = li.present? && li.max_quantity.to_i != variant_data[:max_quantity].to_i
|
||||
|
||||
li_added || li_quantity_changed || li_max_quantity_changed
|
||||
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
|
||||
@@ -83,4 +93,8 @@ Spree::OrderPopulator.class_eval do
|
||||
def order_cycle_required_for(variant)
|
||||
variant.product.product_distributions.empty?
|
||||
end
|
||||
|
||||
def line_item_for_variant_id(variant_id)
|
||||
order.find_line_item_by_variant Spree::Variant.find(variant_id)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,6 +45,54 @@ module Spree
|
||||
end
|
||||
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
|
||||
op.should_receive(:line_item_for_variant_id).with(variant.id).and_return(nil)
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '2'}).should be_true
|
||||
end
|
||||
|
||||
it "returns true when item is not in cart and a max_quantity is specified" do
|
||||
op.should_receive(:line_item_for_variant_id).with(variant.id).and_return(nil)
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '0', max_quantity: '2'}).should be_true
|
||||
end
|
||||
|
||||
it "returns false when item is not in cart and no quantity or max_quantity are specified" do
|
||||
op.should_receive(:line_item_for_variant_id).with(variant.id).and_return(nil)
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '0'}).should be_false
|
||||
end
|
||||
|
||||
it "returns true when quantity varies" do
|
||||
li = double(:line_item, quantity: 1, max_quantity: nil)
|
||||
op.stub(:line_item_for_variant_id) { li }
|
||||
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '2'}).should be_true
|
||||
end
|
||||
|
||||
it "returns true when max_quantity varies" do
|
||||
li = double(:line_item, quantity: 1, max_quantity: nil)
|
||||
op.stub(:line_item_for_variant_id) { li }
|
||||
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '1', max_quantity: '3'}).should be_true
|
||||
end
|
||||
|
||||
it "returns false when max_quantity varies only in nil vs 0" do
|
||||
li = double(:line_item, quantity: 1, max_quantity: nil)
|
||||
op.stub(:line_item_for_variant_id) { li }
|
||||
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '1'}).should be_false
|
||||
end
|
||||
|
||||
it "returns false when both are specified and neither varies" do
|
||||
li = double(:line_item, quantity: 1, max_quantity: 2)
|
||||
op.stub(:line_item_for_variant_id) { li }
|
||||
|
||||
op.send(:varies_from_cart, {variant_id: variant.id, quantity: '1', max_quantity: '2'}).should be_false
|
||||
end
|
||||
end
|
||||
|
||||
describe "attempt_cart_add" do
|
||||
it "performs additional validations" do
|
||||
variant = double(:variant)
|
||||
|
||||
Reference in New Issue
Block a user