From 95dbfae7573f35197e338fee0ad0566538fd9ff1 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 22 Nov 2018 15:42:56 +1100 Subject: [PATCH 1/2] Use expect syntax --- spec/support/request/shop_workflow.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/request/shop_workflow.rb b/spec/support/request/shop_workflow.rb index 876f184644..9df7f72126 100644 --- a/spec/support/request/shop_workflow.rb +++ b/spec/support/request/shop_workflow.rb @@ -13,7 +13,7 @@ module ShopWorkflow end def set_order(order) - ApplicationController.any_instance.stub(:session).and_return({order_id: order.id, access_token: order.token}) + allow_any_instance_of(ApplicationController).to receive(:session).and_return({order_id: order.id, access_token: order.token}) end def add_product_to_cart(order, product, quantity: 1) From f4d5727fb455618822f4bbd8415dea35d686434e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 23 Nov 2018 15:38:35 +1100 Subject: [PATCH 2/2] Associate customers with their user records When we introduced the Customer model, we didn't associate any existing customers with users that have the same email address. Later we decided to create that association when users sign up. But we didn't update all the existing customers. We do that now for data consistency and to solve several bugs. --- ...1123012635_associate_customers_to_users.rb | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 db/migrate/20181123012635_associate_customers_to_users.rb diff --git a/db/migrate/20181123012635_associate_customers_to_users.rb b/db/migrate/20181123012635_associate_customers_to_users.rb new file mode 100644 index 0000000000..b42739ff68 --- /dev/null +++ b/db/migrate/20181123012635_associate_customers_to_users.rb @@ -0,0 +1,42 @@ +# When we introduced the Customer model, we didn't associate any existing +# customers with users that have the same email address. +# Later we decided to create that association when users sign up. But we didn't +# update all the existing customers. We do that now for data consistency and to +# solve several bugs. +# +# - https://github.com/openfoodfoundation/openfoodnetwork/pull/2084 +# - https://github.com/openfoodfoundation/openfoodnetwork/issues/2841 +class AssociateCustomersToUsers < ActiveRecord::Migration + class Customer < ActiveRecord::Base + end + + def up + save_customers + execute "UPDATE customers + SET user_id = spree_users.id + FROM spree_users + WHERE customers.email = spree_users.email + AND customers.user_id IS NULL;" + end + + def down + customers = backed_up_customers + Customer.where(id: customers).update_all(user_id: nil) + end + + def save_customers + customers = Customer. + joins("INNER JOIN spree_users ON customers.email = spree_users.email"). + where(user_id: nil).all + + File.write(backup_file, Marshal.dump(customers)) + end + + def backed_up_customers + Marshal.load(File.read(backup_file)) + end + + def backup_file + File.join("log", "customers_without_user_association.log") + end +end