From 4c754e2cdbde8004c3b3e703ff535fbc8b635afd Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 1 Dec 2016 13:49:15 +1100 Subject: [PATCH] Ensuring that new standing_line_items are added to orders and totals are updated --- app/forms/standing_order_form.rb | 17 ++++++++++++++-- spec/forms/standing_order_form_spec.rb | 28 ++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index ddf39ca316..d61a4e9cfa 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -35,6 +35,15 @@ class StandingOrderForm updateable_line_items(sli).update_all(quantity: sli.quantity) # Avoid validation end + new_standing_line_items.each do |sli| + future_and_undated_orders.each do |order| + line_item = order.line_items.create(variant_id: sli.variant_id, quantity: 0) + line_item.update_attribute(:quantity, sli.quantity) # Avoid validation + end + end + + future_and_undated_orders.each(&:save) + standing_order.save end end @@ -50,7 +59,7 @@ class StandingOrderForm def future_and_undated_orders return @future_and_undated_orders unless @future_and_undated_orders.nil? - @future_and_undated_orders = orders.joins(:order_cycle).merge(OrderCycle.not_closed) + @future_and_undated_orders = orders.joins(:order_cycle).merge(OrderCycle.not_closed).readonly(false) end def create_order_for(order_cycle_id) @@ -121,7 +130,11 @@ class StandingOrderForm end def changed_standing_line_items - standing_line_items.select(&:changed?) + standing_line_items.select{ |sli| sli.changed? && sli.persisted? } + end + + def new_standing_line_items + standing_line_items.select(&:new_record?) end def updateable_line_items(sli) diff --git a/spec/forms/standing_order_form_spec.rb b/spec/forms/standing_order_form_spec.rb index 61a3da8f0f..e506836eaf 100644 --- a/spec/forms/standing_order_form_spec.rb +++ b/spec/forms/standing_order_form_spec.rb @@ -210,11 +210,35 @@ module OpenFoodNetwork let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 4} ] } } let(:form) { StandingOrderForm.new(standing_order, params) } - before { form.save } + before { form.send(:initialise_orders!) } - it "updates the quantity on all orders" do + it "updates the line_item quantities and totals on all orders" do + expect(standing_order.orders.first.reload.total.to_f).to eq 59.97 + form.save line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: sli.variant_id) expect(line_items.map(&:quantity)).to eq [4] + expect(standing_order.orders.first.reload.total.to_f).to eq 119.94 + end + end + + describe "adding a new line item" do + let!(:standing_order) { create(:standing_order_with_items) } + let!(:variant) { create(:variant) } + let!(:order_cycle) { standing_order.schedule.order_cycles.first } + let!(:params) { { standing_line_items_attributes: [ { id: nil, variant_id: variant.id, quantity: 1} ] } } + let!(:form) { StandingOrderForm.new(standing_order, params) } + + before do + order_cycle.variants << variant + form.send(:initialise_orders!) + end + + it "add the line item and updates the total on all orders" do + expect(standing_order.orders.first.reload.total.to_f).to eq 59.97 + form.save + line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: variant.id) + expect(line_items.map(&:quantity)).to eq [1] + expect(standing_order.orders.first.reload.total.to_f).to eq 79.96 end end