Skip to content

Commit 38ef7a7

Browse files
Revert "refactor product and variant model for restrict destroy if line items present. Change flash to show error of product if not destroyed in admin/products_controller"
This reverts commit f2e3447.
1 parent a607b1e commit 38ef7a7

File tree

5 files changed

+27
-8
lines changed

5 files changed

+27
-8
lines changed

backend/app/controllers/spree/admin/products_controller.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ def destroy
4747

4848
begin
4949
# TODO: why is @product.destroy raising ActiveRecord::RecordNotDestroyed instead of failing with false result
50-
# Issue found for above comment: https://github.com/rails/rails/issues/19761
5150
if @product.destroy
5251
flash[:success] = Spree.t('notice_messages.product_deleted')
5352
else
54-
flash[:error] = @object.errors.full_messages.join(', ')
53+
flash[:error] = Spree.t('notice_messages.product_not_deleted')
5554
end
5655
rescue ActiveRecord::RecordNotDestroyed => e
5756
flash[:error] = Spree.t('notice_messages.product_not_deleted')

backend/spec/controllers/spree/admin/products_controller_spec.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,10 @@ def send_request
6262
end
6363

6464
context 'will not successfully destroy product' do
65-
let!(:error_message) { 'Test error' }
66-
6765
before do
6866
allow(Spree::Product).to receive(:friendly).and_return(products)
6967
allow(products).to receive(:find).with(product.id.to_s).and_return(product)
7068
allow(product).to receive(:destroy).and_return(false)
71-
allow(product).to receive_message_chain(:errors, :full_messages).and_return([error_message])
7269
end
7370

7471
describe 'expects to receive' do
@@ -87,7 +84,7 @@ def send_request
8784
describe 'response' do
8885
before { send_request }
8986
it { expect(response).to have_http_status(:ok) }
90-
it { expect(flash[:error]).to eq(error_message) }
87+
it { expect(flash[:error]).to eq(Spree.t('notice_messages.product_not_deleted')) }
9188
end
9289
end
9390
end

core/app/models/spree/product.rb

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class Product < Spree::Base
8888
after_save :run_touch_callbacks, if: :anything_changed?
8989
after_save :reset_nested_changes
9090
after_touch :touch_taxons
91+
before_destroy :ensure_no_line_items
9192

9293
before_validation :normalize_slug, on: :update
9394
before_validation :validate_master
@@ -356,6 +357,13 @@ def touch_taxons
356357
Spree::Taxonomy.where(id: taxonomy_ids).update_all(updated_at: Time.current)
357358
end
358359

360+
def ensure_no_line_items
361+
if line_items.any?
362+
errors.add(:base, Spree.t(:cannot_destroy_if_attached_to_line_items))
363+
return false
364+
end
365+
end
366+
359367
def remove_taxon(taxon)
360368
removed_classifications = classifications.where(taxon: taxon)
361369
removed_classifications.each &:remove_from_list

core/app/models/spree/variant.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ class Variant < Spree::Base
1414

1515
with_options inverse_of: :variant do
1616
has_many :inventory_units
17+
has_many :line_items
1718
has_many :stock_items, dependent: :destroy
1819
end
1920

20-
has_many :line_items, dependent: :restrict_with_error
21-
2221
has_many :orders, through: :line_items
2322
with_options through: :stock_items do
2423
has_many :stock_locations
@@ -49,6 +48,7 @@ class Variant < Spree::Base
4948

5049
after_create :create_stock_items
5150
after_create :set_master_out_of_stock, unless: :is_master?
51+
before_destroy :ensure_no_line_items
5252

5353
after_touch :clear_in_stock_cache
5454

@@ -258,6 +258,13 @@ def discontinued?
258258

259259
private
260260

261+
def ensure_no_line_items
262+
if line_items.any?
263+
errors.add(:base, Spree.t(:cannot_destroy_if_attached_to_line_items))
264+
return false
265+
end
266+
end
267+
261268
def quantifier
262269
Spree::Stock::Quantifier.new(self)
263270
end

core/config/locales/en.yml

+8
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ en:
313313
attributes:
314314
expires_at:
315315
invalid_date_range: must be later than start date
316+
spree/product:
317+
attributes:
318+
base:
319+
cannot_destroy_if_attached_to_line_items: Cannot delete products once they are attached to line items.
316320
spree/refund:
317321
attributes:
318322
amount:
@@ -346,6 +350,10 @@ en:
346350
attributes:
347351
base:
348352
cannot_destroy_if_attached_to_line_items: Cannot delete variants once they are attached to line items.
353+
spree/shipping_method:
354+
attributes:
355+
base:
356+
cannot_destroy_if_attached_to_line_items: Cannot delete variants once they are attached to line items.
349357

350358
devise:
351359
confirmations:

0 commit comments

Comments
 (0)