Skip to content

Commit b997d79

Browse files
authored
Merge pull request #6309 from mamhoff/customer-return-dependent
Add foreign key and handling for stock location deletion to customer returns
2 parents e5930a6 + 3557e7a commit b997d79

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

core/app/models/spree/customer_return.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Spree
44
class CustomerReturn < Spree::Base
55
include Metadata
66

7-
belongs_to :stock_location, optional: true
7+
belongs_to :stock_location
88

99
has_many :return_items, inverse_of: :customer_return
1010
has_many :return_authorizations, through: :return_items
@@ -14,7 +14,6 @@ class CustomerReturn < Spree::Base
1414
before_create :generate_number
1515

1616
validates :return_items, presence: true
17-
validates :stock_location, presence: true
1817
validate :return_items_belong_to_same_order
1918

2019
accepts_nested_attributes_for :return_items

core/app/models/spree/stock_location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class InvalidMovementError < StandardError; end
1515
has_many :stock_movements, through: :stock_items
1616
has_many :user_stock_locations, dependent: :delete_all
1717
has_many :users, through: :user_stock_locations
18+
has_many :customer_returns, inverse_of: :stock_location, dependent: :restrict_with_error
1819

1920
belongs_to :state, class_name: 'Spree::State', optional: true
2021
belongs_to :country, class_name: 'Spree::Country', optional: true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozent_string_literal: true
2+
#
3+
class AddFkToCustomerReturn < ActiveRecord::Migration[7.0]
4+
FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException]
5+
6+
def up
7+
# Uncomment the following code to remove orphaned records if this migration fails
8+
#
9+
# say_with_time "Removing invalid adjustment reason IDs from adjustments table" do
10+
# Spree::CustomerReturn.where.not(stock_location_id: nil).left_joins(:stock_location).where(spree_stock_locations: { id: nil }).delete_all
11+
# end
12+
13+
add_foreign_key :spree_customer_returns, :spree_stock_locations, column: :stock_location_id, null: false, on_delete: :restrict
14+
rescue ActiveRecord::StatementInvalid => e
15+
if e.cause.class.name.in?(FOREIGN_KEY_VIOLATION_ERRORS)
16+
say <<~MSG
17+
⚠️ Foreign key constraint failed when adding :spree_customer_returns => :spree_stock_locations.
18+
To fix this:
19+
1. Uncomment the code that removes invalid adjustment reason IDs from the spree_customer_returns table.
20+
2. Rerun the migration.
21+
Offending error: #{e.cause.class} - #{e.cause.message}
22+
MSG
23+
end
24+
raise
25+
end
26+
27+
def down
28+
remove_foreign_key :spree_customer_returns, :spree_stock_locations, column: :stock_location_id, null: false, on_delete: :restrict
29+
end
30+
end

core/spec/models/spree/stock_location_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44

55
module Spree
66
RSpec.describe StockLocation, type: :model do
7-
subject { create(:stock_location_with_items, backorderable_default: true) }
7+
subject(:stock_location) { create(:stock_location_with_items, backorderable_default: true) }
88
let(:stock_item) { subject.stock_items.order(:id).first }
99
let(:variant) { stock_item.variant }
1010

1111
it 'creates stock_items for all variants' do
1212
expect(subject.stock_items.count).to eq Variant.count
1313
end
1414

15+
describe "#customer_returns" do
16+
let(:customer_return) { create(:customer_return, stock_location: stock_location) }
17+
18+
it "works" do
19+
expect(stock_location.customer_returns).to include(customer_return)
20+
end
21+
end
22+
1523
context "handling stock items" do
1624
let!(:variant) { create(:variant) }
1725

0 commit comments

Comments
 (0)