File tree 5 files changed +27
-8
lines changed
app/controllers/spree/admin
spec/controllers/spree/admin
5 files changed +27
-8
lines changed Original file line number Diff line number Diff line change @@ -47,11 +47,10 @@ def destroy
47
47
48
48
begin
49
49
# 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
51
50
if @product . destroy
52
51
flash [ :success ] = Spree . t ( 'notice_messages.product_deleted' )
53
52
else
54
- flash [ :error ] = @object . errors . full_messages . join ( ', ')
53
+ flash [ :error ] = Spree . t ( 'notice_messages.product_not_deleted ')
55
54
end
56
55
rescue ActiveRecord ::RecordNotDestroyed => e
57
56
flash [ :error ] = Spree . t ( 'notice_messages.product_not_deleted' )
Original file line number Diff line number Diff line change @@ -62,13 +62,10 @@ def send_request
62
62
end
63
63
64
64
context 'will not successfully destroy product' do
65
- let! ( :error_message ) { 'Test error' }
66
-
67
65
before do
68
66
allow ( Spree ::Product ) . to receive ( :friendly ) . and_return ( products )
69
67
allow ( products ) . to receive ( :find ) . with ( product . id . to_s ) . and_return ( product )
70
68
allow ( product ) . to receive ( :destroy ) . and_return ( false )
71
- allow ( product ) . to receive_message_chain ( :errors , :full_messages ) . and_return ( [ error_message ] )
72
69
end
73
70
74
71
describe 'expects to receive' do
@@ -87,7 +84,7 @@ def send_request
87
84
describe 'response' do
88
85
before { send_request }
89
86
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' ) ) }
91
88
end
92
89
end
93
90
end
Original file line number Diff line number Diff line change @@ -88,6 +88,7 @@ class Product < Spree::Base
88
88
after_save :run_touch_callbacks , if : :anything_changed?
89
89
after_save :reset_nested_changes
90
90
after_touch :touch_taxons
91
+ before_destroy :ensure_no_line_items
91
92
92
93
before_validation :normalize_slug , on : :update
93
94
before_validation :validate_master
@@ -356,6 +357,13 @@ def touch_taxons
356
357
Spree ::Taxonomy . where ( id : taxonomy_ids ) . update_all ( updated_at : Time . current )
357
358
end
358
359
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
+
359
367
def remove_taxon ( taxon )
360
368
removed_classifications = classifications . where ( taxon : taxon )
361
369
removed_classifications . each &:remove_from_list
Original file line number Diff line number Diff line change @@ -14,11 +14,10 @@ class Variant < Spree::Base
14
14
15
15
with_options inverse_of : :variant do
16
16
has_many :inventory_units
17
+ has_many :line_items
17
18
has_many :stock_items , dependent : :destroy
18
19
end
19
20
20
- has_many :line_items , dependent : :restrict_with_error
21
-
22
21
has_many :orders , through : :line_items
23
22
with_options through : :stock_items do
24
23
has_many :stock_locations
@@ -49,6 +48,7 @@ class Variant < Spree::Base
49
48
50
49
after_create :create_stock_items
51
50
after_create :set_master_out_of_stock , unless : :is_master?
51
+ before_destroy :ensure_no_line_items
52
52
53
53
after_touch :clear_in_stock_cache
54
54
@@ -258,6 +258,13 @@ def discontinued?
258
258
259
259
private
260
260
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
+
261
268
def quantifier
262
269
Spree ::Stock ::Quantifier . new ( self )
263
270
end
Original file line number Diff line number Diff line change 313
313
attributes :
314
314
expires_at :
315
315
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.
316
320
spree/refund :
317
321
attributes :
318
322
amount :
346
350
attributes :
347
351
base :
348
352
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.
349
357
350
358
devise :
351
359
confirmations :
You can’t perform that action at this time.
0 commit comments