mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-26 05:55:15 +00:00
Finish refactoring of Standing Order Updaters, replacing with form class used by controller
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 })
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user