Remove StockItem#stock_location

This commit is contained in:
Maikel Linke
2025-01-21 16:08:45 +11:00
parent 87d20877ad
commit d5ff1f5c71
4 changed files with 4 additions and 29 deletions

View File

@@ -2,12 +2,14 @@
module Spree
class StockItem < ApplicationRecord
self.ignored_columns += [:stock_location_id]
acts_as_paranoid
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
has_many :stock_movements, dependent: :destroy
validates :variant_id, uniqueness: { scope: [:stock_location_id, :deleted_at] }
validates :variant_id, uniqueness: { scope: [:deleted_at] }
validates :count_on_hand, numericality: { greater_than_or_equal_to: 0, unless: :backorderable? }
delegate :weight, to: :variant
@@ -39,14 +41,6 @@ module Spree
self[:count_on_hand] = value
end
# Other code still calls this.
# TODO: remove usage
def stock_location
@stock_location ||= DefaultStockLocation.find_or_create
end
attr_writer :stock_location
private
def process_backorders

View File

@@ -6,7 +6,7 @@ module Api
attributes :id, :name, :producer_name, :image, :sku, :import_date, :tax_category_id,
:options_text, :unit_value, :unit_description, :unit_to_display,
:display_as, :display_name, :name_to_display, :variant_overrides_count,
:price, :on_demand, :on_hand, :in_stock, :stock_location_id, :stock_location_name,
:price, :on_demand, :on_hand, :in_stock,
:variant_unit, :variant_unit_scale, :variant_unit_name, :variant_unit_with_scale
has_one :primary_taxon, key: :category_id, embed: :id
@@ -44,18 +44,6 @@ module Api
object.in_stock?
end
def stock_location_id
return if object.stock_items.empty?
options[:stock_location]&.id || object.stock_items.first.stock_location.id
end
def stock_location_name
return if object.stock_items.empty?
options[:stock_location]&.name || object.stock_items.first.stock_location.name
end
def variant_overrides_count
object.variant_overrides.count
end

View File

@@ -8,7 +8,6 @@ class CatalogItemBuilder < DfcBuilder
if variant.stock_items.empty?
variant.stock_items << Spree::StockItem.new(
stock_location: DefaultStockLocation.find_or_create,
variant:,
)
end

View File

@@ -22,10 +22,4 @@ RSpec.describe Api::Admin::VariantSerializer do
expect(serializer.to_json).to match variant.full_name
end
it "serializes the variant stock location id" do
serializer = Api::Admin::VariantSerializer.new variant
expect(serializer.to_json).to match variant.stock_items.first.stock_location.id.to_s
end
end