Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve setting user addresses when adding new addresses during checkout (#12286)

Damian Legawiec0703ce29f1b2
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 9e0185bf13..39a84aee9b 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -89,7 +89,7 @@ module Spree
acts_as_taggable_on :tags
acts_as_taggable_tenant :store_id
- belongs_to :user, class_name: "::#{Spree.user_class}", optional: true
+ belongs_to :user, class_name: "::#{Spree.user_class}", optional: true, autosave: true
belongs_to :created_by, class_name: "::#{Spree.admin_user_class}", optional: true
belongs_to :approver, class_name: "::#{Spree.admin_user_class}", optional: true
belongs_to :canceler, class_name: "::#{Spree.admin_user_class}", optional: true
diff --git a/core/app/models/spree/order/address_book.rb b/core/app/models/spree/order/address_book.rb
index b03e28a832..0fb07e8c64 100644
--- a/core/app/models/spree/order/address_book.rb
+++ b/core/app/models/spree/order/address_book.rb
@@ -2,12 +2,14 @@ module Spree
class Order < Spree.base_class
module AddressBook
def clone_shipping_address
- self.bill_address_id = ship_address_id if ship_address_id
+ self.bill_address = ship_address if ship_address
+ user.bill_address = ship_address if should_assign_user_default_address?(ship_address)
true
end
def clone_billing_address
- self.ship_address_id = bill_address_id if bill_address_id
+ self.ship_address = bill_address if bill_address
+ user.ship_address = bill_address if should_assign_user_default_address?(bill_address)
true
end
@@ -23,12 +25,7 @@ module Spree
def bill_address_attributes=(attributes)
self.bill_address = update_or_create_address(attributes)
-
- if should_assign_user_default_address?(bill_address)
- user_old_address = user&.bill_address
- user_old_address&.delete unless user_old_address&.valid?
- user&.update(bill_address: bill_address)
- end
+ user.bill_address = bill_address if should_assign_user_default_address?(bill_address)
end
def ship_address_id=(id)
@@ -43,12 +40,7 @@ module Spree
def ship_address_attributes=(attributes)
self.ship_address = update_or_create_address(attributes)
-
- if should_assign_user_default_address?(ship_address)
- user_old_address = user&.ship_address
- user_old_address&.delete unless user_old_address&.valid?
- user&.update(ship_address: ship_address)
- end
+ user.ship_address = ship_address if should_assign_user_default_address?(ship_address)
end
private
@@ -87,7 +79,7 @@ module Spree
end
def should_assign_user_default_address?(address)
- address.present? && address.valid?
+ user.present? && address.present? && address.valid? && address.user == user
end
end
end
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 7137760a0f..8128681dbb 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -42,15 +42,14 @@ describe Spree::Order, type: :model do
describe '#clone_shipping_address' do
it 'clones the shipping address when use_shipping is true' do
- order.use_shipping = true
- order.save!
- expect(order.ship_address).to eq(order.bill_address)
+ order.update!(use_shipping: true)
+ expect(order.reload.bill_address).to eq(ship_address)
+ expect(user.reload.bill_address).to eq(ship_address)
end
it 'does not clone the shipping address when use_shipping is false' do
- order.use_shipping = false
- order.save!
- expect(order.ship_address).not_to eq(order.bill_address)
+ order.update!(use_shipping: false)
+ expect(order.reload.bill_address).not_to eq(order.ship_address)
end
end
end
@@ -1567,28 +1566,6 @@ describe Spree::Order, type: :model do
end
end
- context 'when user\'s default address is invalid' do
- let(:invalid_address) { build(:address, firstname: nil, lastname: nil, address1: nil, phone: nil) }
- let(:user) { create(:user, bill_address_id: invalid_address.id) }
-
- before do
- invalid_address.save(validate: false)
- user
- order.reload
- end
-
- it 'deletes it and assign new address' do
- expect(user.bill_address_id).to eq invalid_address.id
-
- subject
-
- expect(user.reload.bill_address_id).to be_present
- expect(user.bill_address_id).to_not eq(invalid_address.id)
-
- expect { invalid_address.reload }.to raise_error ActiveRecord::RecordNotFound
- end
- end
-
context 'when user has no default address' do
let!(:user) { create(:user) }
@@ -1663,28 +1640,6 @@ describe Spree::Order, type: :model do
end
end
- context 'when user\'s default address is invalid' do
- let(:invalid_address) { build(:address, firstname: nil, lastname: nil, address1: nil, phone: nil) }
- let(:user) { create(:user, ship_address_id: invalid_address.id) }
-
- before do
- invalid_address.save(validate: false)
- user
- order.reload
- end
-
- it 'deletes it and assign new address' do
- expect(user.ship_address_id).to eq invalid_address.id
-
- subject
-
- expect(user.reload.ship_address_id).to be_present
- expect(user.ship_address_id).not_to eq(invalid_address.id)
-
- expect { invalid_address.reload }.to raise_error ActiveRecord::RecordNotFound
- end
- end
-
context 'when user has no default address' do
let!(:user) { create(:user) }