From 0cf7d1a63da10b97efebf9679bb87badcdc3d609 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 15 Dec 2016 13:11:21 +1100 Subject: [PATCH] Link standing order orders to user on initialisation if customer user exists --- app/models/proxy_order.rb | 1 + spec/models/proxy_order_spec.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/models/proxy_order.rb b/app/models/proxy_order.rb index e1a7acd8a8..b8ebb7e555 100644 --- a/app/models/proxy_order.rb +++ b/app/models/proxy_order.rb @@ -45,6 +45,7 @@ class ProxyOrder < ActiveRecord::Base distributor_id: standing_order.shop_id, shipping_method_id: standing_order.shipping_method_id, }) + order.update_attribute(:user, standing_order.customer.user) standing_order.standing_line_items.each do |sli| order.line_items.build(variant_id: sli.variant_id, quantity: sli.quantity, skip_stock_check: true) end diff --git a/spec/models/proxy_order_spec.rb b/spec/models/proxy_order_spec.rb index 32cd8f78d6..67684b030a 100644 --- a/spec/models/proxy_order_spec.rb +++ b/spec/models/proxy_order_spec.rb @@ -149,6 +149,8 @@ describe ProxyOrder, type: :model do expect(proxy_order.reload.order).to be_a Spree::Order order = proxy_order.order expect(order.line_items.count).to eq standing_order.standing_line_items.count + expect(order.customer).to eq standing_order.customer + expect(order.user).to eq standing_order.customer.user expect(order.distributor).to eq standing_order.shop expect(order.order_cycle).to eq proxy_order.order_cycle expect(order.shipping_method).to eq standing_order.shipping_method @@ -175,6 +177,19 @@ describe ProxyOrder, type: :model do expect(order.line_items.find_by_variant_id(variant.id).quantity).to eq 5 end end + + context "when the customer does not have a user associated with it" do + before do + standing_order.customer.update_attribute(:user_id, nil) + end + + it "initialises the order without a user_id" do + expect{ proxy_order.initialise_order! }.to change{Spree::Order.count}.by(1) + expect(proxy_order.reload.order).to be_a Spree::Order + order = proxy_order.order + expect(order.user).to be nil + end + end end context "when the order has already been initialised" do