From 2a3dc33f868359ca62193e626613ac6efd5439c6 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 9 Nov 2016 10:47:25 +1100 Subject: [PATCH] WIP: Pushing shipment and payment update logic down into separate methods --- app/forms/standing_order_form.rb | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/app/forms/standing_order_form.rb b/app/forms/standing_order_form.rb index 855ca889d9..934eb05592 100644 --- a/app/forms/standing_order_form.rb +++ b/app/forms/standing_order_form.rb @@ -24,21 +24,8 @@ class StandingOrderForm orders.update_all(customer_id: customer_id, email: customer.andand.email, distributor_id: shop_id) orders.each do |order| - if shipping_method_id_changed? - shipment = order.shipments.with_state('pending').where(shipping_method_id: shipping_method_id_was).last - if shipment - shipment.update_attributes(shipping_method_id: shipping_method_id) - order.update_attribute(:shipping_method_id, shipping_method_id) - end - end - - if payment_method_id_changed? - payment = order.payments.with_state('checkout').where(payment_method_id: payment_method_id_was).last - if payment - payment.andand.void_transaction! - create_payment_for(order) - end - end + update_shipment_for(order) if shipping_method_id_changed? + update_payment_for(order) if payment_method_id_changed? end standing_order.save @@ -80,6 +67,22 @@ class StandingOrderForm order.payments.create(payment_method_id: payment_method_id, amount: order.reload.total) end + def update_payment_for(order) + payment = order.payments.with_state('checkout').where(payment_method_id: payment_method_id_was).last + if payment + payment.andand.void_transaction! + create_payment_for(order) + end + end + + def update_shipment_for(order) + shipment = order.shipments.with_state('pending').where(shipping_method_id: shipping_method_id_was).last + if shipment + shipment.update_attributes(shipping_method_id: shipping_method_id) + order.update_attribute(:shipping_method_id, shipping_method_id) + end + end + def initialise_orders! uninitialised_order_cycle_ids.each do |order_cycle_id| orders << create_order_for(order_cycle_id)