Skip to content

Commit 518a777

Browse files
authored
Merge pull request solidusio#3852 from mamhoff/remove-archived-user-addresses
Remove Spree::UserAddress#archived flag
2 parents b367f99 + 6fc2564 commit 518a777

File tree

6 files changed

+45
-96
lines changed

6 files changed

+45
-96
lines changed

api/openapi/solidus-api.oas.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,10 +1169,7 @@ paths:
11691169
'422':
11701170
$ref: '#/components/responses/delete-restriction'
11711171
summary: Remove address from user address book
1172-
description: |-
1173-
Removes an address from a user's address book.
1174-
1175-
**Note:** Rather than delete a `Spree::UserAddress` record this action set its `archived` attribute to `true`.
1172+
description: Removes an address from a user's address book.
11761173
operationId: remove-address-from-user-address-book
11771174
tags:
11781175
- Address books
@@ -1201,11 +1198,7 @@ paths:
12011198
operationId: update-user-address-book
12021199
tags:
12031200
- Address books
1204-
description: |-
1205-
Updates a user's address book.
1206-
1207-
**Note:** if the passed `id` matches an existing `address` a new `Spree::Address` record will be created and the matched `address` `archived` on `Spree::UserAddress`. For a similar logic, if the passed `id` matches an existing `address` which is in `archived` state, the `Spree::UserAddress#archived` record will be restored to `false`.
1208-
See `user_address_book.rb` for further information.
1201+
description: Updates a user's address book.
12091202
security:
12101203
- api-key: []
12111204
requestBody:

api/spec/requests/spree/api/address_books_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ module Spree::Api
5959
put "/api/users/#{user.id}/address_book",
6060
params: { address_book: harry_address_attributes.merge('id' => address.id) },
6161
headers: { Authorization: 'Bearer galleon' }
62-
}.to change { Spree::UserAddress.count }.from(1).to(2)
62+
}.to change { Spree::Address.count }.from(1).to(2)
63+
64+
expect {
65+
put "/api/users/#{user.id}/address_book",
66+
params: { address_book: harry_address_attributes.merge('id' => address.id) },
67+
headers: { Authorization: 'Bearer galleon' }
68+
}.not_to change { Spree::UserAddress.count }.from(1)
6369

6470
expect(response.status).to eq(200)
6571
expect(JSON.parse(response.body).first).to include(harry_address_attributes)
@@ -119,7 +125,7 @@ module Spree::Api
119125
end
120126
end
121127

122-
it 'archives my address' do
128+
it 'removes the address from my address book' do
123129
address = create(:address)
124130
user = create(:user, spree_api_key: 'galleon')
125131
user.save_in_address_book(address.attributes, false)
@@ -168,13 +174,19 @@ module Spree::Api
168174
put "/api/users/#{other_user.id}/address_book",
169175
params: { address_book: updated_harry_address.merge('id' => address.id) },
170176
headers: { Authorization: 'Bearer galleon' }
171-
}.to change { Spree::UserAddress.count }.from(1).to(2)
177+
}.to change { Spree::Address.count }.from(1).to(2)
178+
179+
expect {
180+
put "/api/users/#{other_user.id}/address_book",
181+
params: { address_book: updated_harry_address.merge('id' => address.id) },
182+
headers: { Authorization: 'Bearer galleon' }
183+
}.not_to change { Spree::UserAddress.count }.from(1)
172184

173185
expect(response.status).to eq(200)
174186
expect(JSON.parse(response.body).first).to include(updated_harry_address)
175187
end
176188

177-
it "archives another user's address" do
189+
it "removes the address from the other user's address book" do
178190
address = create(:address)
179191
other_user = create(:user)
180192
other_user.save_in_address_book(address.attributes, false)

core/app/models/concerns/spree/user_address_book.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module UserAddressBook
55
extend ActiveSupport::Concern
66

77
included do
8-
has_many :user_addresses, -> { active }, foreign_key: "user_id", class_name: "Spree::UserAddress" do
8+
has_many :user_addresses, foreign_key: "user_id", class_name: "Spree::UserAddress" do
99
def find_first_by_address_values(address_attrs)
1010
detect { |ua| ua.address == Spree::Address.new(address_attrs) }
1111
end
@@ -22,7 +22,7 @@ def mark_default(user_address, address_type: :shipping)
2222
end
2323

2424
if user_address.persisted?
25-
user_address.update!(column_for_default => true, archived: false)
25+
user_address.update!(column_for_default => true)
2626
else
2727
user_address.write_attribute(column_for_default, true)
2828
end
@@ -151,7 +151,7 @@ def remove_from_address_book(address_id)
151151
user_address = user_addresses.find_by(address_id:)
152152
if user_address
153153
remove_user_address_reference(address_id)
154-
user_address.update(archived: true, default: false)
154+
user_address.destroy!
155155
else
156156
false
157157
end
@@ -160,10 +160,9 @@ def remove_from_address_book(address_id)
160160
private
161161

162162
def prepare_user_address(new_address)
163-
user_address = user_addresses.all_historical.find_first_by_address_values(new_address.attributes)
163+
user_address = user_addresses.find_first_by_address_values(new_address.attributes)
164164
user_address ||= user_addresses.build
165165
user_address.address = new_address
166-
user_address.archived = false
167166
user_address
168167
end
169168

core/app/models/spree/user_address.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@ class UserAddress < Spree::Base
66
belongs_to :address, class_name: "Spree::Address", optional: true
77

88
validates_uniqueness_of :address_id, scope: :user_id
9-
validates_uniqueness_of :user_id, conditions: -> { active.default_shipping }, message: :default_address_exists, if: :default?
9+
validates_uniqueness_of :user_id, conditions: -> { default_shipping }, message: :default_address_exists, if: :default?
1010

1111
scope :with_address_values, ->(address_attributes) do
1212
joins(:address).merge(
1313
Spree::Address.with_values(address_attributes)
1414
)
1515
end
1616

17-
scope :all_historical, -> { unscope(where: :archived) }
17+
scope :all_historical, -> {
18+
Spree::Deprecation.warn("This scope does not do anything and will be removed from Solidus 5.")
19+
all
20+
}
1821
scope :default_shipping, -> { where(default: true) }
1922
scope :default_billing, -> { where(default_billing: true) }
20-
scope :active, -> { where(archived: false) }
23+
scope :active, -> {
24+
Spree::Deprecation.warn("This scope does not do anything and will be removed from Solidus 5.")
25+
all
26+
}
2127

2228
default_scope -> { order([default: :desc, updated_at: :desc]) }
2329
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
class RemoveArchivedUserAddresses < ActiveRecord::Migration[5.2]
4+
def up
5+
Spree::UserAddress.where(archived: true).delete_all
6+
remove_column :spree_user_addresses, :archived, :boolean, default: false
7+
end
8+
9+
def down
10+
add_column :spree_user_addresses, :archived, :boolean, default: false
11+
end
12+
end

core/spec/models/spree/concerns/user_address_book_spec.rb

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -225,78 +225,6 @@ module Spree
225225
end
226226
end
227227
end
228-
229-
context "resurrecting a previously saved (but now archived) address" do
230-
let(:address) { create(:address) }
231-
before do
232-
user.save_in_address_book(address.attributes, true)
233-
user.remove_from_address_book(address.id)
234-
end
235-
subject { user.save_in_address_book(address.attributes, true) }
236-
237-
it "returns the address" do
238-
expect(subject).to eq address
239-
end
240-
241-
context "when called with default address_type" do
242-
it "sets the passed address as default shipping address" do
243-
subject
244-
expect(user.ship_address).to eq address
245-
end
246-
end
247-
248-
context "when called with address_type = :billing" do
249-
subject { user.save_in_address_book(address.attributes, true, :billing) }
250-
251-
it "sets the passed address as default billing address" do
252-
subject
253-
expect(user.bill_address).to eq address
254-
end
255-
end
256-
257-
context "via an edit to another address" do
258-
let(:address2) { create(:address, name: "Different") }
259-
let(:edited_attributes) do
260-
# conceptually edit address2 to match the values of address
261-
edited_attributes = address.attributes
262-
edited_attributes[:id] = address2.id
263-
edited_attributes
264-
end
265-
266-
before { user.save_in_address_book(address2.attributes, true) }
267-
268-
subject { user.save_in_address_book(edited_attributes) }
269-
270-
it "returns the address" do
271-
expect(subject).to eq address
272-
end
273-
274-
it "archives address2" do
275-
subject
276-
user_address_two = user.user_addresses.all_historical.find_by(address_id: address2.id)
277-
expect(user_address_two.archived).to be true
278-
end
279-
280-
context "via a new address that matches an archived one" do
281-
let(:added_attributes) do
282-
added_attributes = address.attributes
283-
added_attributes.delete(:id)
284-
added_attributes
285-
end
286-
287-
subject { user.save_in_address_book(added_attributes) }
288-
289-
it "returns the address" do
290-
expect(subject).to eq address
291-
end
292-
293-
it "no longer has archived user_addresses" do
294-
subject
295-
expect(user.user_addresses.all_historical).to eq user.user_addresses
296-
end
297-
end
298-
end
299-
end
300228
end
301229

302230
context "#remove_from_address_book" do
@@ -316,10 +244,9 @@ module Spree
316244
expect(user.user_addresses.find_first_by_address_values(address1.attributes)).to be_nil
317245
end
318246

319-
it "leaves user_address record in an archived state" do
247+
it "deletes user_address record" do
320248
subject
321-
archived_user_address = user.user_addresses.all_historical.find_first_by_address_values(address1.attributes)
322-
expect(archived_user_address).to be_archived
249+
expect(user.user_addresses.map(&:address)).to eq([address2])
323250
end
324251

325252
it "returns false if the addresses is not there" do

0 commit comments

Comments
 (0)