From 3eff65f1f58695afe1bff05fcaf11e5b5225d7c8 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 9 Nov 2016 14:06:01 +1100 Subject: [PATCH] Finish refactoring of Standing Order Updaters, replacing with form class used by controller --- app/forms/standing_order_form.rb | 19 +++++++++- app/models/standing_line_item.rb | 6 ---- .../standing_line_item_updater.rb | 35 ------------------- spec/forms/standing_order_form_spec.rb | 7 ++-- 4 files changed, 23 insertions(+), 44 deletions(-) delete mode 100644 lib/open_food_network/standing_line_item_updater.rb diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index 934eb05592..872a986b90 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -28,6 +28,10 @@ class StandingOrderForm update_payment_for(order) if payment_method_id_changed? end + changed_standing_line_items.each do |sli| + updateable_line_items(sli).update_all(quantity: sli.quantity) + end + standing_order.save end end @@ -42,7 +46,8 @@ class StandingOrderForm private def future_and_undated_orders - orders.joins(:order_cycle).merge(OrderCycle.not_closed) + return @future_and_undated_orders unless @future_and_undated_orders.nil? + @future_and_undated_orders = orders.joins(:order_cycle).merge(OrderCycle.not_closed) end def create_order_for(order_cycle_id) @@ -92,4 +97,16 @@ class StandingOrderForm def uninitialised_order_cycle_ids order_cycles.pluck(:id) - orders.map(&:order_cycle_id) end + + def changed_standing_line_items + standing_line_items.select(&:changed?) + end + + def updateable_line_items(sli) + line_items_from_future_and_undated_orders(sli.variant_id).where(quantity: sli.quantity_was) + end + + def line_items_from_future_and_undated_orders(variant_id) + Spree::LineItem.where(order_id: future_and_undated_orders, variant_id: variant_id) + end end diff --git a/app/models/standing_line_item.rb b/app/models/standing_line_item.rb index 7e72dc6f41..6f4372d4cf 100644 --- a/app/models/standing_line_item.rb +++ b/app/models/standing_line_item.rb @@ -1,8 +1,4 @@ -require 'open_food_network/standing_line_item_updater' - class StandingLineItem < ActiveRecord::Base - include OpenFoodNetwork::StandingLineItemUpdater - belongs_to :standing_order, inverse_of: :standing_line_items belongs_to :variant, class_name: 'Spree::Variant' @@ -10,8 +6,6 @@ class StandingLineItem < ActiveRecord::Base validates :variant, presence: true validates :quantity, { presence: true, numericality: { only_integer: true } } - # before_save :update_line_items! # In OpenFoodNetwork::StandingLineItemUpdater - def available_from?(shop, schedule) Spree::Variant.joins(exchanges: { order_cycle: :schedules}) .where(id: variant_id, schedules: { id: schedule}, exchanges: { incoming: false, receiver_id: shop }) diff --git a/lib/open_food_network/standing_line_item_updater.rb b/lib/open_food_network/standing_line_item_updater.rb deleted file mode 100644 index 1834448bde..0000000000 --- a/lib/open_food_network/standing_line_item_updater.rb +++ /dev/null @@ -1,35 +0,0 @@ -module OpenFoodNetwork - module StandingLineItemUpdater - - attr_accessor :altered_orders - - def update_line_items! - altered_orders ||= [] - attr_names.each do |attr_name| - unaltered_line_items(attr_name).update_all(:"#{attr_name}" => send(attr_name)) - if altered_line_items(attr_name).any? - altered_orders |= altered_line_items(attr_name).map(&:order) - end - end - end - - private - - def attr_names - [:quantity] - end - - def unaltered_line_items(attr_name) - return line_items_from_future_and_undated_orders unless persisted? - line_items_from_future_and_undated_orders.where("#{attr_name} = (?)", send("#{attr_name}_was")) - end - - def altered_line_items(attr_name) - line_items_from_future_and_undated_orders - unaltered_line_items(attr_name) - end - - def line_items_from_future_and_undated_orders - Spree::LineItem.where(order_id: standing_order.future_and_undated_orders, variant_id: variant_id) - end - end -end diff --git a/spec/forms/standing_order_form_spec.rb b/spec/forms/standing_order_form_spec.rb index 00e59b3324..d910ac163a 100644 --- a/spec/forms/standing_order_form_spec.rb +++ b/spec/forms/standing_order_form_spec.rb @@ -140,10 +140,13 @@ module OpenFoodNetwork describe "changing the quantity of a line item" do let(:standing_order) { create(:standing_order_with_items) } + let(:sli) { standing_order.standing_line_items.first } + let(:params) { { standing_line_items_attributes: [ { id: sli.id, quantity: 4} ] } } + let(:form) { StandingOrderForm.new(standing_order, params) } + + before { form.save } it "updates the quantity on all orders" do - sli = standing_order.standing_line_items.first - sli.update_attribute(:quantity, 4) line_items = Spree::LineItem.where(order_id: standing_order.orders, variant_id: sli.variant_id) expect(line_items.map(&:quantity)).to eq [4] end