Remove unused StockLocation#stock_items

And the reverse association.
This commit is contained in:
Maikel Linke
2025-01-17 15:03:39 +11:00
parent 6dfa184a15
commit aa9daed66e
12 changed files with 13 additions and 38 deletions

View File

@@ -4,9 +4,6 @@ module Spree
class StockItem < ApplicationRecord
acts_as_paranoid
# WIP: phasing out stock location, it's not used.
belongs_to :stock_location, class_name: 'Spree::StockLocation', inverse_of: :stock_items,
optional: true
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
has_many :stock_movements, dependent: :destroy
@@ -48,6 +45,8 @@ module Spree
@stock_location ||= DefaultStockLocation.find_or_create
end
attr_writer :stock_location
private
def process_backorders

View File

@@ -5,8 +5,6 @@ module Spree
self.belongs_to_required_by_default = false
self.ignored_columns += [:backorderable_default, :active]
has_many :stock_items, dependent: :delete_all, inverse_of: :stock_location
belongs_to :state, class_name: 'Spree::State'
belongs_to :country, class_name: 'Spree::Country'
@@ -16,11 +14,5 @@ module Spree
def stock_item(variant)
StockItem.where(variant_id: variant).order(:id).first
end
# We have only one default stock location and it may be unpersisted.
# So all stock items belong to any unpersisted stock location.
def stock_items
StockItem.all
end
end
end

View File

@@ -39,7 +39,6 @@ module Spree
has_many :line_items, inverse_of: :variant, dependent: nil
has_many :stock_items, dependent: :destroy, inverse_of: :variant
has_many :stock_locations, through: :stock_items
has_many :images, -> { order(:position) }, as: :viewable,
dependent: :destroy,
class_name: "Spree::Image"

View File

@@ -61,7 +61,7 @@ class ProductScopeQuery
def product_query_includes
[
image: { attachment_attachment: :blob },
variants: [:default_price, :stock_locations, :stock_items, :variant_overrides]
variants: [:default_price, :stock_items, :variant_overrides]
]
end

View File

@@ -117,7 +117,7 @@ class ProductsRenderer
# rubocop:disable Rails/FindEach # .each returns an array, .find_each returns nil
distributed_products.variants_relation.
includes(:default_price, :stock_locations, :product).
includes(:default_price, :product).
where(product_id: products).
each { |v| scoper.scope(v) } # Scope results with variant_overrides
# rubocop:enable Rails/FindEach

View File

@@ -42,10 +42,8 @@ RSpec.describe Api::V0::ProductsController, type: :controller do
api_get :show, id: product.to_param
expect(all_attributes.all?{ |attr| json_response.keys.include? attr }).to eq(true)
expect(variants_attributes.all?{ |attr|
json_response['variants'].first.keys.include? attr
} ).to eq(true)
expect(json_response.keys).to include(*all_attributes)
expect(json_response["variants"].first.keys).to include(*variants_attributes)
end
it "returns a 404 error when it cannot find a product" do

View File

@@ -83,7 +83,6 @@ RSpec.describe VariantStock do
it 'returns false' do
variant = build_stubbed(
:variant,
stock_locations: [build_stubbed(:stock_location)]
)
expect(variant.on_demand).to be_falsy
end
@@ -94,9 +93,6 @@ RSpec.describe VariantStock do
let(:variant) do
build_stubbed(
:variant,
stock_locations: [
build_stubbed(:stock_location)
]
)
end
@@ -148,7 +144,6 @@ RSpec.describe VariantStock do
build_stubbed(
:variant,
on_demand: true,
stock_locations: [build_stubbed(:stock_location)]
)
end
let(:stock_item) { Spree::StockItem.new(backorderable: true) }
@@ -172,7 +167,6 @@ RSpec.describe VariantStock do
build_stubbed(
:variant,
on_demand: false,
stock_locations: [build_stubbed(:stock_location)]
)
end

View File

@@ -3,8 +3,8 @@
require 'spec_helper'
RSpec.describe Spree::InventoryUnit do
let(:stock_location) { create(:stock_location_with_items) }
let(:stock_item) { stock_location.stock_items.order(:id).first }
let!(:stock_location) { create(:stock_location_with_items) }
let(:stock_item) { Spree::StockItem.order(:id).first }
context "#backordered_for_stock_item" do
let(:order) { create(:order) }

View File

@@ -3,13 +3,11 @@
require 'spec_helper'
RSpec.describe Spree::StockItem do
let(:stock_location) { create(:stock_location_with_items) }
let!(:stock_location) { create(:stock_location_with_items) }
subject { stock_location.stock_items.order(:id).first }
subject(:stock_item) { Spree::StockItem.order(:id).first }
describe "validation" do
let(:stock_item) { stock_location.stock_items.first }
it "requires count_on_hand to be positive if not backorderable" do
stock_item.backorderable = false

View File

@@ -5,13 +5,9 @@ require 'spec_helper'
module Spree
RSpec.describe StockLocation do
subject { create(:stock_location_with_items) }
let(:stock_item) { subject.stock_items.order(:id).first }
let(:stock_item) { StockItem.order(:id).first }
let(:variant) { stock_item.variant }
it 'creates stock_items for all variants' do
expect(subject.stock_items.count).to eq Variant.count
end
it 'finds a stock_item for a variant' do
stock_item = subject.stock_item(variant)
expect(stock_item.count_on_hand).to eq 15

View File

@@ -3,8 +3,8 @@
require 'spec_helper'
RSpec.describe Spree::StockMovement do
let(:stock_location) { create(:stock_location_with_items) }
let(:stock_item) { stock_location.stock_items.order(:id).first }
let!(:stock_location) { create(:stock_location_with_items) }
let(:stock_item) { Spree::StockItem.order(:id).first }
subject { build(:stock_movement, stock_item:) }
it 'should belong to a stock item' do

View File

@@ -12,7 +12,6 @@ RSpec.describe Spree::Variant do
it { is_expected.to have_many(:inventory_units) }
it { is_expected.to have_many(:line_items) }
it { is_expected.to have_many(:stock_items) }
it { is_expected.to have_many(:stock_locations).through(:stock_items) }
it { is_expected.to have_many(:images) }
it { is_expected.to have_one(:default_price) }
it { is_expected.to have_many(:prices) }