Skip to content

Commit 3557e7a

Browse files
mamhofftvdeyen
authored andcommitted
Add Spree::StockLocation#customer_returns
We have a foreign key here, but no `has_many` declaration. Because customer returns are important, and potentially carry return items, this needs to be present so we can present the user with a good error message when trying to delete a stock location with existing customer returns.
1 parent 6a3ccb5 commit 3557e7a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

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)