Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Phase P0.3: Move core call sites onto stock levels
Łukasz Adamczyk2705a8d0de23
diff --git a/docs/plans/6.0-typed-stock-movements.md b/docs/plans/6.0-typed-stock-movements.mdindex 3969a91ab0..d410763bb1 100644--- a/docs/plans/6.0-typed-stock-movements.md+++ b/docs/plans/6.0-typed-stock-movements.md@@ -361,6 +361,7 @@ Ruby side:- `Spree::StockLevel` with `has_prefix_id :sl`. `Spree::StockItem = Spree::StockLevel` survives as a **deprecated** constant alias in its own file, the same shape `spree/core/app/models/spree/shipment.rb` already uses for `Spree::Shipment = Fulfillment`, with a comment naming 6.1 as its removal. Phase 5 deletes it.- Associations renamed with deprecated aliases for one release: `Variant#stock_levels`, `StockLocation#stock_levels`, `Store#stock_levels`, `StockMovement#stock_level`, `StockReservation#stock_level`, plus `StockLocation#stock_level_or_create` and `#set_up_stock_level`.+- The service and job namespace becomes `Spree::StockLocations::StockLevels`, and the old one survives as deprecated shims the way `Spree::Taxons::AddProducts` and `Spree::Products::TouchTaxonsJob` already do: the service warns and delegates, the job **subclasses** the renamed job so a job enqueued under the old class name before the deploy still deserializes and runs (added 2026-08-17 — the first pass dropped these, which would have failed those in-flight jobs).- `alias_attribute :stock_item_id, :stock_level_id` on movements and reservations, so an integration still writing the old key keeps working through 6.0.- `Spree::StockLevel::CustomEvents` wraps `publish_event` so the three lifecycle events dual-emit under both names, exactly as `Fulfillment::CustomEvents` already dual-emits `shipment.*` (decision revised 2026-08-17). The earlier draft put a `legacy_event_prefix:` option on `publishes_lifecycle_events`; that is generic machinery on a concern every Spree model includes, bought for a single rename, so it was reverted. The twins do not live in the `Spree::StockItem` alias file either, tempting as that is for keeping the bridges together: `app/` is autoloaded lazily, so with `eager_load = false` the file would go untouched and the legacy events would fire in production and silently not in development. Wrapping `publish_event` rather than the three callbacks keeps Publishable's touch-only guard authoritative — the legacy twin follows whatever the real name did.- The Ransack allowlists, the `:stock` permission subject list, the factory (`:stock_level`, with `:stock_item` kept as an alias for external specs), and the `activerecord.models` and admin-label i18n keys.@@ -467,6 +468,7 @@ Held to 6.1 for the reason every other 6.0 cleanup is: the columns are the data| `alias_attribute :stock_item_id, :stock_level_id` on movements and reservations | Delete. Writes using the old key start raising instead of resolving. || `stock_item_serializer` / `admin_stock_item_serializer` registry keys | Drop from the legacy-key map. || `:stock_item` factory alias | Delete — external specs move to `:stock_level`. |+| `Spree::StockLocations::StockItems::Create` / `::CreateJob` shims | Delete both files. By 6.1 no queue can still hold a job enqueued under the old name. |Nothing generic survives this bridge: the twins are three lines in a model-owned concern, so deleting that file is the whole removal. A future rename writes its own, the way `Fulfillment::CustomEvents` did before it — one rename is not enough evidence to design an abstraction every Spree model carries.diff --git a/spree/core/app/jobs/spree/stock_locations/stock_items/create_job.rb b/spree/core/app/jobs/spree/stock_locations/stock_items/create_job.rbindex 70e5e2ac1f..e24415ac2f 100644--- a/spree/core/app/jobs/spree/stock_locations/stock_items/create_job.rb+++ b/spree/core/app/jobs/spree/stock_locations/stock_items/create_job.rb@@ -1,11 +1,13 @@module Spreemodule StockLocationsmodule StockItems- class CreateJob < Spree::BaseJob- queue_as Spree.queues.stock_location_stock_items-- def perform(stock_location)- Spree::StockLocations::StockItems::Create.call(stock_location: stock_location)+ # @deprecated Renamed to Spree::StockLocations::StockLevels::CreateJob in 6.0.+ # Subclasses it so jobs enqueued under the old class name before the deploy+ # still deserialize and run; removed in 6.1.+ class CreateJob < StockLevels::CreateJob+ def perform(*args)+ Spree::Deprecation.warn('Spree::StockLocations::StockItems::CreateJob is deprecated and will be removed in Spree 6.1. Use Spree::StockLocations::StockLevels::CreateJob instead.')+ superendendenddiff --git a/spree/core/app/jobs/spree/stock_locations/stock_levels/create_job.rb b/spree/core/app/jobs/spree/stock_locations/stock_levels/create_job.rbnew file mode 100644index 0000000000..3d954d2684--- /dev/null+++ b/spree/core/app/jobs/spree/stock_locations/stock_levels/create_job.rb@@ -0,0 +1,13 @@+module Spree+ module StockLocations+ module StockLevels+ class CreateJob < Spree::BaseJob+ queue_as Spree.queues.stock_location_stock_levels++ def perform(stock_location)+ Spree::StockLocations::StockLevels::Create.call(stock_location: stock_location)+ end+ end+ end+ end+enddiff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rbindex b09f764487..3a818c391f 100644--- a/spree/core/app/models/concerns/spree/product_scopes.rb+++ b/spree/core/app/models/concerns/spree/product_scopes.rb@@ -46,21 +46,21 @@ module Spreewhere(Price.table_name => { amount: low..high })}- # Joins spree_variants and spree_stock_items directly (without association+ # Joins spree_variants and spree_stock_levels directly (without association# aliases) so that the table names stay as-is. This avoids alias conflicts# when combined with other scopes (e.g., price sorting) that also join# spree_variants through associations which generate aliases.- def self.join_variants_and_stock_items+ def self.join_variants_and_stock_levelsjoins("INNER JOIN #{Variant.table_name} ON #{Variant.table_name}.deleted_at IS NULL AND #{Variant.table_name}.product_id = #{Product.table_name}.id").- joins("LEFT OUTER JOIN #{StockItem.table_name} ON #{StockItem.table_name}.deleted_at IS NULL AND #{StockItem.table_name}.variant_id = #{Variant.table_name}.id")+ joins("LEFT OUTER JOIN #{StockLevel.table_name} ON #{StockLevel.table_name}.deleted_at IS NULL AND #{StockLevel.table_name}.variant_id = #{Variant.table_name}.id")end- private_class_method :join_variants_and_stock_items+ private_class_method :join_variants_and_stock_levels# Mirrors Spree::Variant.in_stock_or_backorderable logic using raw table- # names (to pair with join_variants_and_stock_items).+ # names (to pair with join_variants_and_stock_levels).scope :in_stock_or_backorderable_condition, -> {where(- "#{Variant.table_name}.track_inventory = ? OR #{StockItem.table_name}.count_on_hand > ? OR #{StockItem.table_name}.backorderable = ?",+ "#{Variant.table_name}.track_inventory = ? OR #{StockLevel.table_name}.count_on_hand > ? OR #{StockLevel.table_name}.backorderable = ?",false, 0, true)}@@ -71,7 +71,7 @@ module Spreeif in_stock == '0' || !in_stockallelse- join_variants_and_stock_items.in_stock_or_backorderable_condition+ join_variants_and_stock_levels.in_stock_or_backorderable_conditionendend@@ -87,16 +87,16 @@ module Spreeif out_of_stock == '0' || !out_of_stockallelse- where.not(id: join_variants_and_stock_items.in_stock_or_backorderable_condition)+ where.not(id: join_variants_and_stock_levels.in_stock_or_backorderable_condition)endenddef self.backorderable- join_variants_and_stock_items.where(StockItem.table_name => { backorderable: true })+ join_variants_and_stock_levels.where(StockLevel.table_name => { backorderable: true })enddef self.in_stock_or_backorderable- join_variants_and_stock_items.in_stock_or_backorderable_condition+ join_variants_and_stock_levels.in_stock_or_backorderable_conditionend# This scope selects products in taxon AND all its descendantsdiff --git a/spree/core/app/models/spree/exports/products.rb b/spree/core/app/models/spree/exports/products.rbindex 9687376a2e..b9b9020987 100644--- a/spree/core/app/models/spree/exports/products.rb+++ b/spree/core/app/models/spree/exports/products.rb@@ -9,7 +9,7 @@ module Spreeenddef variant_includes- [:images, :prices, :stock_items, :stock_locations, { option_values: [:option_type] }]+ [:images, :prices, :stock_levels, :stock_locations, { option_values: [:option_type] }]enddef multi_line_csv?diff --git a/spree/core/app/models/spree/fulfillment_item.rb b/spree/core/app/models/spree/fulfillment_item.rbindex 2502ff6315..9a9cccb620 100644--- a/spree/core/app/models/spree/fulfillment_item.rb+++ b/spree/core/app/models/spree/fulfillment_item.rb@@ -20,10 +20,10 @@ module Spreescope :on_hand_or_backordered, -> { where status: ['backordered', 'on_hand'] }scope :shipped, -> { where status: 'shipped' }scope :returned, -> { where status: 'returned' }- scope :backordered_per_variant, ->(stock_item) do+ scope :backordered_per_variant, ->(stock_level) doincludes(:fulfillment, :order).where.not(Spree::Fulfillment.table_name => { status: 'canceled' }).- where(variant_id: stock_item.variant_id).+ where(variant_id: stock_level.variant_id).where.not(spree_orders: { completed_at: nil }).backordered.order('spree_orders.completed_at ASC')end@@ -60,9 +60,9 @@ module Spree# objects## Returns an array of backordered inventory units as per a given stock item- def self.backordered_for_stock_item(stock_item)- backordered_per_variant(stock_item).select do |unit|- unit.fulfillment.stock_location == stock_item.stock_location+ def self.backordered_for_stock_level(stock_level)+ backordered_per_variant(stock_level).select do |unit|+ unit.fulfillment.stock_location == stock_level.stock_locationendend@@ -70,8 +70,8 @@ module Spreeupdate_all(pending: false, updated_at: Time.current)end- def find_stock_item- fulfillment.stock_location.stock_item_or_create(variant)+ def find_stock_level+ fulfillment.stock_location.stock_level_or_create(variant)enddef self.split(original_inventory_unit, extract_quantity)diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex 6cf75c6596..d028f9f893 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -586,8 +586,8 @@ module Spreedef backordered_variantsvariants.where(track_inventory: true).- joins(:stock_items, :product).- where(Spree::StockItem.table_name => { count_on_hand: ..0, backorderable: true })+ joins(:stock_levels, :product).+ where(Spree::StockLevel.table_name => { count_on_hand: ..0, backorderable: true })enddef can_ship?diff --git a/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb b/spree/core/app/models/spree/order_routing/rules/minimize_splits.rbindex e6045096d6..b13f819bf1 100644--- a/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb+++ b/spree/core/app/models/spree/order_routing/rules/minimize_splits.rb@@ -8,7 +8,7 @@ module Spreeclass MinimizeSplits < Spree::OrderRoutingRuledef rank(order, locations)demand = required_quantity_by_variant(order)- counts = stock_item_counts(demand.keys, locations)+ counts = stock_level_counts(demand.keys, locations)locations.map do |loc|coverage = demand.count do |variant_id, qty|@@ -30,11 +30,11 @@ module Spreeend# One query for the entire location × variant matrix instead of- # N variants × M locations stock_item lookups.- def stock_item_counts(variant_ids, locations)+ # N variants × M locations stock_level lookups.+ def stock_level_counts(variant_ids, locations)return {} if variant_ids.empty? || locations.empty?- Spree::StockItem+ Spree::StockLevel.where(stock_location_id: locations.map(&:id), variant_id: variant_ids).pluck(:stock_location_id, :variant_id, :count_on_hand).each_with_object({}) { |(loc_id, var_id, count), h| h[[loc_id, var_id]] = count }diff --git a/spree/core/app/models/spree/order_routing/strategy/rules.rb b/spree/core/app/models/spree/order_routing/strategy/rules.rbindex 3b1d70d2a4..8b162cde79 100644--- a/spree/core/app/models/spree/order_routing/strategy/rules.rb+++ b/spree/core/app/models/spree/order_routing/strategy/rules.rb@@ -41,8 +41,8 @@ module Spreedef eligible_locationsorder.store.stock_locations.active- .joins(:stock_items)- .where(spree_stock_items: { variant_id: requested_variant_ids })+ .joins(:stock_levels)+ .where(spree_stock_levels: { variant_id: requested_variant_ids }).distinct.to_aenddiff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex ef7b9760d9..3208296f38 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -160,7 +160,7 @@ module Spreehas_many :prices, through: :variants- has_many :stock_items, through: :variants+ has_many :stock_levels, through: :variantshas_many :line_items, through: :variantshas_many :orders, through: :line_items@@ -258,7 +258,7 @@ module Spree:variants,allow_destroy: true,reject_if: lambda do |v|- v[:option_value_variants_attributes].blank? && v[:stock_items_attributes].blank? && v[:prices_attributes].blank?+ v[:option_value_variants_attributes].blank? && v[:stock_levels_attributes].blank? && v[:prices_attributes].blank?end)accepts_nested_attributes_for(@@ -614,11 +614,17 @@ module Spreeif variants.loaded?variants.sum(&:total_on_hand)else- stock_items.loaded? ? stock_items.sum(&:count_on_hand) : stock_items.sum(:count_on_hand)+ stock_levels.loaded? ? stock_levels.sum(&:count_on_hand) : stock_levels.sum(:count_on_hand)endendend+ # @deprecated Use {#stock_levels}; removed in 6.1.+ def stock_items+ Spree::Deprecation.warn('Spree::Product#stock_items is deprecated and will be removed in Spree 6.1. Use #stock_levels instead.')+ stock_levels+ end+# @deprecated Use #default_variant — the master variant is being removed in Spree 6.1.def masterSpree::Deprecation.warn('Spree::Product#master is deprecated and will be removed in Spree 6.1. Use #default_variant instead.')diff --git a/spree/core/app/models/spree/stock/coordinator.rb b/spree/core/app/models/spree/stock/coordinator.rbindex e3341390d9..1398345de4 100644--- a/spree/core/app/models/spree/stock/coordinator.rb+++ b/spree/core/app/models/spree/stock/coordinator.rb@@ -42,8 +42,8 @@ module Spreeprivatedef stock_locations_with_requested_variants- order.store.stock_locations.active.joins(:stock_items).- where(spree_stock_items: { variant_id: requested_variant_ids }).distinct+ order.store.stock_locations.active.joins(:stock_levels).+ where(spree_stock_levels: { variant_id: requested_variant_ids }).distinctend# An item may only be allocated from locations its delivery profilediff --git a/spree/core/app/models/spree/stock/quantifier.rb b/spree/core/app/models/spree/stock/quantifier.rbindex 92dada0513..df64e6a418 100644--- a/spree/core/app/models/spree/stock/quantifier.rb+++ b/spree/core/app/models/spree/stock/quantifier.rb@@ -33,7 +33,7 @@ module Spree# Physical pool minus already-allocated units, summed across the# variant's active stock items.#- # In Spree 5.5 {Spree::StockItem#allocated_count} is a Ruby shim that+ # In Spree 5.5 {Spree::StockLevel#allocated_count} is a Ruby shim that# always returns 0, so this equals +SUM(count_on_hand)+. In 6.0# (Typed Stock Movements) +allocated_count+ becomes a real column and# the SQL path subtracts it natively.@@ -41,11 +41,11 @@ module Spree# @return [Integer] units available before checkout reservationsdef available_stockif association_loaded?- stock_items.sum(&:available_count)+ stock_levels.sum(&:available_count)elsif self.class.allocated_count_column?- stock_items.sum('count_on_hand - allocated_count')+ stock_levels.sum('count_on_hand - allocated_count')else- stock_items.sum(:count_on_hand)+ stock_levels.sum(:count_on_hand)endend@@ -53,7 +53,7 @@ module Spree# location-filtered stock items. Returns 0 when stock reservations# are globally disabled.#- # Reads through the same {#stock_items} collection as {#available_stock}+ # Reads through the same {#stock_levels} collection as {#available_stock}# so a per-location query (filtered by `stock_location`) only counts# reservations that belong to those same stock items — otherwise a# multi-location variant would subtract reservations from other@@ -67,19 +67,19 @@ module Spreedef reserved_quantityreturn @reserved_quantity if defined?(@reserved_quantity)return @reserved_quantity = 0 unless reservations_enabled?- return @reserved_quantity = 0 if stock_items.blank?+ return @reserved_quantity = 0 if stock_levels.blank?excluded_owner_key = excluded_order.is_a?(Spree::Cart) ? :cart_id : :order_idexcluded_owner_id = excluded_order&.id@reserved_quantity = if reservations_preloaded?- stock_items.sum do |si|+ stock_levels.sum do |si|reservations = si.active_stock_reservationsreservations = reservations.reject { |r| r.public_send(excluded_owner_key) == excluded_owner_id } if excluded_owner_idreservations.sum(&:quantity)endelse- reservations = Spree::StockReservation.active.where(stock_item_id: stock_items.map(&:id))+ reservations = Spree::StockReservation.active.where(stock_level_id: stock_levels.map(&:id))reservations = reservations.where.not(excluded_owner_key => excluded_owner_id) if excluded_owner_idreservations.sum(:quantity)end@@ -87,7 +87,7 @@ module Spree# Check if any of variant stock items is backorderabledef backorderable?- @backorderable ||= stock_items.any?(&:backorderable)+ @backorderable ||= stock_levels.any?(&:backorderable)end# Whether the requested quantity can be supplied. Beyond on-hand stock a@@ -114,8 +114,8 @@ module Spreesupplyable >= requiredend- def stock_items- @stock_items ||= scope_to_location(variant.stock_items)+ def stock_levels+ @stock_levels ||= scope_to_location(variant.stock_levels)end# Memoized schema check so {#available_stock} doesn't introspect the@@ -126,7 +126,7 @@ module Spreedef self.allocated_count_column?return @allocated_count_column if defined?(@allocated_count_column)- @allocated_count_column = Spree::StockItem.connection.column_exists?(:spree_stock_items, :allocated_count)+ @allocated_count_column = Spree::StockLevel.connection.column_exists?(:spree_stock_levels, :allocated_count)endprivate@@ -140,12 +140,12 @@ module Spreeenddef association_loaded?- variant.association(:stock_items).loaded?+ variant.association(:stock_levels).loaded?enddef reservations_preloaded?association_loaded? &&- stock_items.all? { |si| si.association(:active_stock_reservations).loaded? }+ stock_levels.all? { |si| si.association(:active_stock_reservations).loaded? }enddef scope_to_location(collection)diff --git a/spree/core/app/models/spree/stock_level.rb b/spree/core/app/models/spree/stock_level.rbindex 9d1ff86254..27fbaccc9d 100644--- a/spree/core/app/models/spree/stock_level.rb+++ b/spree/core/app/models/spree/stock_level.rb@@ -11,7 +11,7 @@ module Spreepublishes_lifecycle_events- with_options inverse_of: :stock_items do+ with_options inverse_of: :stock_levels dobelongs_to :stock_location, class_name: 'Spree::StockLocation'belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'end@@ -53,7 +53,7 @@ module Spree}def backordered_inventory_units- Spree::InventoryUnit.backordered_for_stock_item(self)+ Spree::InventoryUnit.backordered_for_stock_level(self)enddef adjust_count_on_hand(value)diff --git a/spree/core/app/models/spree/stock_location.rb b/spree/core/app/models/spree/stock_location.rbindex a6faf5cb10..8b29862fc1 100644--- a/spree/core/app/models/spree/stock_location.rb+++ b/spree/core/app/models/spree/stock_location.rb@@ -21,9 +21,9 @@ module Spreehas_many :fulfillments, class_name: 'Spree::Fulfillment'has_many :shipments, class_name: 'Spree::Fulfillment', foreign_key: :stock_location_id, deprecated: true- has_many :stock_items, class_name: 'Spree::StockLevel', dependent: :delete_all, inverse_of: :stock_location- has_many :variants, through: :stock_items- has_many :stock_movements, through: :stock_items+ has_many :stock_levels, class_name: 'Spree::StockLevel', dependent: :delete_all, inverse_of: :stock_location+ has_many :variants, through: :stock_levels+ has_many :stock_movements, through: :stock_levelshas_iso_geography@@ -42,7 +42,7 @@ module Spreescope :pickup_enabled, -> { where(pickup_enabled: true) }scope :order_default, -> { order(default: :desc, name: :asc) }- after_create :create_stock_items, if: :propagate_all_variants?+ after_create :create_stock_levels, if: :propagate_all_variants?after_save :ensure_one_defaultafter_update :conditional_touch_records@@ -54,35 +54,59 @@ module Spree# Wrapper for creating a new stock item respecting the backorderable configdef propagate_variant(variant)- stock_items.create!(variant: variant, backorderable: backorderable_default)+ stock_levels.create!(variant: variant, backorderable: backorderable_default)end# Return either an existing stock item or create a new one. Useful in# scenarios where the user might not know whether there is already a stock# item for a given variant- def set_up_stock_item(variant)- stock_item(variant) || propagate_variant(variant)+ def set_up_stock_level(variant)+ stock_level(variant) || propagate_variant(variant)end- # Returns an instance of StockItem for the variant id.+ # Returns an instance of StockLevel for the variant id.## @param variant_id [String] The id of a variant.#- # @return [StockItem] Corresponding StockItem for the StockLocation's variant.- def stock_item(variant_id)- stock_items.where(variant_id: variant_id).order(:id).first+ # @return [StockLevel] Corresponding StockLevel for the StockLocation's variant.+ def stock_level(variant_id)+ stock_levels.where(variant_id: variant_id).order(:id).firstenddef stocks?(variant)- stock_items.exists?(variant: variant)+ stock_levels.exists?(variant: variant)+ end++ # @deprecated Use {#stock_levels}; removed in 6.1.+ def stock_items+ Spree::Deprecation.warn('Spree::StockLocation#stock_items is deprecated and will be removed in Spree 6.1. Use #stock_levels instead.')+ stock_levels+ end++ # @deprecated Use {#stock_level}; removed in 6.1.+ def stock_item(variant_id)+ Spree::Deprecation.warn('Spree::StockLocation#stock_item is deprecated and will be removed in Spree 6.1. Use #stock_level instead.')+ stock_level(variant_id)end- # Attempts to look up StockItem for the variant, and creates one if not found.+ # @deprecated Use {#set_up_stock_level}; removed in 6.1.+ def set_up_stock_item(variant)+ Spree::Deprecation.warn('Spree::StockLocation#set_up_stock_item is deprecated and will be removed in Spree 6.1. Use #set_up_stock_level instead.')+ set_up_stock_level(variant)+ end++ # @deprecated Use {#stock_level_or_create}; removed in 6.1.+ def stock_item_or_create(variant_or_variant_id)+ Spree::Deprecation.warn('Spree::StockLocation#stock_item_or_create is deprecated and will be removed in Spree 6.1. Use #stock_level_or_create instead.')+ stock_level_or_create(variant_or_variant_id)+ end++ # Attempts to look up StockLevel for the variant, and creates one if not found.## @param variant Variant instance or Variant ID#- # @return [StockItem] Corresponding StockItem for the StockLocation's variant.- def stock_item_or_create(variant_or_variant_id)+ # @return [StockLevel] Corresponding StockLevel for the StockLocation's variant.+ def stock_level_or_create(variant_or_variant_id)if variant_or_variant_id.is_a?(Spree::Variant)variant_id = variant_or_variant_id.idvariant = variant_or_variant_id@@ -90,7 +114,7 @@ module Spreevariant_id = variant_or_variant_idvariant = Spree::Variant.find(variant_or_variant_id)end- stock_item(variant_id) || propagate_variant(variant)+ stock_level(variant_id) || propagate_variant(variant)end# Returns the count on hand number for the variant@@ -99,11 +123,11 @@ module Spree## @return [Integer]def count_on_hand(variant)- stock_item(variant).try(:count_on_hand)+ stock_level(variant).try(:count_on_hand)enddef backorderable?(variant)- stock_item(variant).try(:backorderable?)+ stock_level(variant).try(:backorderable?)enddef restock(variant, quantity, originator = nil, persist: true)@@ -111,7 +135,7 @@ module Spreeenddef restock_backordered(variant, quantity, _originator = nil)- item = stock_item_or_create(variant)+ item = stock_level_or_create(variant)item.update_columns(count_on_hand: item.count_on_hand + quantity,updated_at: Time.current@@ -123,17 +147,17 @@ module Spreeenddef move(variant, quantity, originator = nil, persist: true)- stock_item = stock_item_or_create(variant)+ stock_level = stock_level_or_create(variant)if persist- stock_item.stock_movements.create!(quantity: quantity, originator: originator)+ stock_level.stock_movements.create!(quantity: quantity, originator: originator)else- originator.stock_movements << stock_item.stock_movements.build(quantity: quantity)+ originator.stock_movements << stock_level.stock_movements.build(quantity: quantity)endenddef fill_status(variant, quantity)- if item = stock_item_or_create(variant)+ if item = stock_level_or_create(variant)if item.count_on_hand >= quantityon_hand = quantitybackordered = 0@@ -190,8 +214,8 @@ module Spreeprivate- def create_stock_items- Spree::StockLocations::StockItems::CreateJob.perform_later(self)+ def create_stock_levels+ Spree::StockLocations::StockLevels::CreateJob.perform_later(self)end# One default per STORE — scoping to the whole table would let one@@ -206,7 +230,7 @@ module Spreedef conditional_touch_recordsreturn unless active_changed?- stock_items.update_all(updated_at: Time.current)+ stock_levels.update_all(updated_at: Time.current)variants.update_all(updated_at: Time.current)endenddiff --git a/spree/core/app/models/spree/stock_transfer.rb b/spree/core/app/models/spree/stock_transfer.rbindex a620bc1293..6b5a8daee7 100644--- a/spree/core/app/models/spree/stock_transfer.rb+++ b/spree/core/app/models/spree/stock_transfer.rb@@ -12,7 +12,7 @@ module Spreehas_many :stock_movements, as: :originatoraccepts_nested_attributes_for :stock_movements, reject_if: proc { |attributes|attributes[:quantity] = attributes[:quantity].to_i- attributes[:quantity].blank? || attributes[:quantity].zero? || attributes[:stock_item_id].blank?+ attributes[:quantity].blank? || attributes[:quantity].zero? || attributes[:stock_level_id].blank?}belongs_to :source_location, class_name: 'StockLocation', optional: true@@ -74,7 +74,7 @@ module Spreeprivatedef find_stock_location_with_location_id(location_id)- stock_movements.joins(:stock_item).where('spree_stock_items.stock_location_id' => location_id)+ stock_movements.joins(:stock_level).where('spree_stock_levels.stock_location_id' => location_id)enddef source_location_is_not_destination_location@@ -92,7 +92,7 @@ module Spreedef variants_available_in_source_location?(source_location, variants)return true if source_location.nil?- source_location.stock_items.where(variant: variants.keys).where(Spree::StockItem.arel_table[:count_on_hand].gt(0)).size == variants.keys.size+ source_location.stock_levels.where(variant: variants.keys).where(Spree::StockLevel.arel_table[:count_on_hand].gt(0)).size == variants.keys.sizeendendenddiff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rbindex 917c748119..961d44e2f8 100644--- a/spree/core/app/models/spree/store.rb+++ b/spree/core/app/models/spree/store.rb@@ -133,7 +133,7 @@ module Spreehas_many :products, class_name: 'Spree::Product', dependent: :nullifyhas_many :product_publications, through: :channels, source: :publications, class_name: 'Spree::ProductPublication'has_many :variants, through: :products, class_name: 'Spree::Variant', source: :variants- has_many :stock_items, through: :variants, class_name: 'Spree::StockLevel'+ has_many :stock_levels, through: :variants, class_name: 'Spree::StockLevel'has_many :prices, through: :variants, class_name: 'Spree::Price'has_many :price_lists, class_name: 'Spree::PriceList', inverse_of: :storehas_many :fulfillment_items, through: :variants, class_name: 'Spree::FulfillmentItem'@@ -163,6 +163,12 @@ module Spreeself.categories = valueend+ # @deprecated Use {#stock_levels}; removed in 6.1.+ def stock_items+ Spree::Deprecation.warn('Spree::Store#stock_items is deprecated and will be removed in Spree 6.1. Use #stock_levels instead.')+ stock_levels+ end+has_many :delivery_zones, class_name: 'Spree::DeliveryZone', dependent: :destroyhas_many :delivery_methods, class_name: 'Spree::DeliveryMethod', dependent: :nullifyhas_many :delivery_profiles, class_name: 'Spree::DeliveryProfile', dependent: :destroydiff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rbindex fa6d5e974b..38f0eb2ae1 100644--- a/spree/core/app/models/spree/variant.rb+++ b/spree/core/app/models/spree/variant.rb@@ -36,12 +36,12 @@ module Spreewith_options inverse_of: :variant dohas_many :fulfillment_items, class_name: 'Spree::FulfillmentItem'has_many :line_items- has_many :stock_items, class_name: 'Spree::StockLevel', dependent: :destroy, autosave: true+ has_many :stock_levels, class_name: 'Spree::StockLevel', dependent: :destroy, autosave: trueendhas_many :inventory_units, class_name: 'Spree::FulfillmentItem', inverse_of: :variant, deprecated: truehas_many :orders, through: :line_items- with_options through: :stock_items do+ with_options through: :stock_levels dohas_many :stock_locationshas_many :stock_movementshas_many :stock_reservations@@ -91,17 +91,17 @@ module Spreevalidates :hs_code, format: { with: /\A\d{6,13}\z/ }, allow_blank: truevalidates :country_of_origin, format: { with: /\A[A-Z]{2}\z/ }, allow_blank: true- after_create :create_stock_items+ after_create :create_stock_levelsafter_commit :clear_line_items_cache, on: :update- after_save :create_default_stock_item, unless: :track_inventory?+ after_save :create_default_stock_level, unless: :track_inventory?after_update_commit :handle_track_inventory_changeafter_create :increment_product_variant_countafter_destroy :decrement_product_variant_count- scope :in_stock, -> { left_joins(:stock_items).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockLevel.table_name}.count_on_hand > ?", false, 0) }- scope :backorderable, -> { left_joins(:stock_items).where(Spree::StockLevel.table_name => { backorderable: true }) }+ scope :in_stock, -> { left_joins(:stock_levels).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockLevel.table_name}.count_on_hand > ?", false, 0) }+ scope :backorderable, -> { left_joins(:stock_levels).where(Spree::StockLevel.table_name => { backorderable: true }) }scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }scope :eligible, -> { all }@@ -172,7 +172,7 @@ module Spreeendaccepts_nested_attributes_for(- :stock_items,+ :stock_levels,reject_if: ->(attributes) { attributes['stock_location_id'].blank? || attributes['count_on_hand'].blank? },allow_destroy: false)@@ -513,22 +513,40 @@ module Spree# Syncs stock items from an array of hashes.# Upserts stock for listed locations, soft-deletes stock items for unlisted locations.# On new records, defers to after_create callback.- # @param stock_items_params [Array<Hash>] array of { stock_location_id:, count_on_hand:, backorderable: }+ # @param stock_levels_params [Array<Hash>] array of { stock_location_id:, count_on_hand:, backorderable: }# @return [void]- def stock_items=(stock_items_params)- return super if stock_items_params.blank? || stock_items_params.first.is_a?(Spree::StockItem)+ def stock_levels=(stock_levels_params)+ return super if stock_levels_params.blank? || stock_levels_params.first.is_a?(Spree::StockLevel)location_ids_in_payload = []- stock_items_params.each do |stock_data|+ stock_levels_params.each do |stock_data|stock_data = stock_data.to_h.with_indifferent_accesslocation = Spree::StockLocation.find_by_param(stock_data[:stock_location_id])location_ids_in_payload << location.idset_stock(stock_data[:count_on_hand], stock_data[:backorderable], location)end- # Soft-delete stock items for locations not in the payload- stock_items.where.not(stock_location_id: location_ids_in_payload).destroy_all if persisted?+ # Soft-delete stock levels for locations not in the payload+ stock_levels.where.not(stock_location_id: location_ids_in_payload).destroy_all if persisted?+ end++ # @deprecated Use {#stock_levels}; removed in 6.1.+ def stock_items+ Spree::Deprecation.warn('Spree::Variant#stock_items is deprecated and will be removed in Spree 6.1. Use #stock_levels instead.')+ stock_levels+ end++ # @deprecated Use {#stock_levels=}; removed in 6.1.+ def stock_items=(value)+ Spree::Deprecation.warn('Spree::Variant#stock_items= is deprecated and will be removed in Spree 6.1. Use #stock_levels= instead.')+ self.stock_levels = value+ end++ # @deprecated Use +stock_levels_attributes=+; removed in 6.1.+ def stock_items_attributes=(value)+ Spree::Deprecation.warn('Spree::Variant#stock_items_attributes= is deprecated and will be removed in Spree 6.1. Use #stock_levels_attributes= instead.')+ self.stock_levels_attributes = valueend# Sets the base price (global price, not for a price list) for the given currency.@@ -596,10 +614,10 @@ module Spree# @return [void]def set_stock(count_on_hand, backorderable = nil, stock_location = nil)stock_location ||= default_stock_location- stock_item = stock_items.find_or_initialize_by(stock_location: stock_location)- stock_item.count_on_hand = count_on_hand- stock_item.backorderable = backorderable if backorderable.present?- stock_item.save! if persisted?+ stock_level = stock_levels.find_or_initialize_by(stock_location: stock_location)+ stock_level.count_on_hand = count_on_hand+ stock_level.backorderable = backorderable if backorderable.present?+ stock_level.save! if persisted?enddef default_stock_location@@ -712,7 +730,7 @@ module Spreeenddef backordered?- @backordered ||= !in_stock? && stock_items.exists?(backorderable: true)+ @backordered ||= !in_stock? && stock_levels.exists?(backorderable: true)end# Is this variant purely digital? (no physical product)@@ -766,7 +784,7 @@ module Spree# Only the product's own store's locations — a new variant must not grow# stock items in every other store's warehouses.- def create_stock_items+ def create_stock_levelslocations = product&.store ? product.store.stock_locations : StockLocation.alllocations.where(propagate_all_variants: true).each do |stock_location|stock_location.propagate_variant(self)@@ -781,17 +799,17 @@ module Spreeline_items.update_all(updated_at: Time.current)end- def create_default_stock_item- return if stock_items.any?+ def create_default_stock_level+ return if stock_levels.any?- Spree::Store.current.default_stock_location.set_up_stock_item(self)+ Spree::Store.current.default_stock_location.set_up_stock_level(self)enddef handle_track_inventory_changereturn unless track_inventory_previously_changed?return if track_inventory- stock_items.update_all(count_on_hand: 0, updated_at: Time.current)+ stock_levels.update_all(count_on_hand: 0, updated_at: Time.current)enddef increment_product_variant_countdiff --git a/spree/core/app/services/spree/carts/remove_out_of_stock_items.rb b/spree/core/app/services/spree/carts/remove_out_of_stock_items.rbindex 593eabee1b..364d617441 100644--- a/spree/core/app/services/spree/carts/remove_out_of_stock_items.rb+++ b/spree/core/app/services/spree/carts/remove_out_of_stock_items.rb@@ -13,7 +13,7 @@ module Spreereturn success([cart, @messages, @warnings]) if cart.total_quantity.zero? || cart.line_items.none?- line_items = cart.line_items.includes(variant: [:product, :stock_locations, { stock_items: [:stock_location, :active_stock_reservations] }])+ line_items = cart.line_items.includes(variant: [:product, :stock_locations, { stock_levels: [:stock_location, :active_stock_reservations] }])removal_error = nildiff --git a/spree/core/app/services/spree/products/duplicator.rb b/spree/core/app/services/spree/products/duplicator.rbindex 5e7a477e69..38ac03b64c 100644--- a/spree/core/app/services/spree/products/duplicator.rb+++ b/spree/core/app/services/spree/products/duplicator.rb@@ -46,7 +46,7 @@ module Spreenew_variant.deleted_at = nilnew_variant.option_values = variant.option_values.map { |option_value| option_value }new_variant.prices = duplicate_prices(variant.prices)- new_variant.stock_items = duplicate_stock_items(variant.stock_items)+ new_variant.stock_levels = duplicate_stock_levels(variant.stock_levels)variant.images.each { |image| duplicate_image(image, new_variant) } if include_images@@ -62,11 +62,11 @@ module Spreeendend- def duplicate_stock_items(stock_items)- stock_items.map do |stock_item|- new_stock_item = stock_item.dup- new_stock_item.count_on_hand = 0- new_stock_item+ def duplicate_stock_levels(stock_levels)+ stock_levels.map do |stock_level|+ new_stock_level = stock_level.dup+ new_stock_level.count_on_hand = 0+ new_stock_levelendenddiff --git a/spree/core/app/services/spree/stock_locations/stock_items/create.rb b/spree/core/app/services/spree/stock_locations/stock_items/create.rbindex ab6660c7a1..1b27648e7c 100644--- a/spree/core/app/services/spree/stock_locations/stock_items/create.rb+++ b/spree/core/app/services/spree/stock_locations/stock_items/create.rb@@ -1,23 +1,18 @@module Spreemodule StockLocationsmodule StockItems+ # @deprecated Renamed to Spree::StockLocations::StockLevels::Create in 6.0.+ # This shim delegates to the renamed service so host code calling the old+ # name keeps working; removed in 6.1.class Createprepend Spree::ServiceModule::Base+ # @param stock_location [Spree::StockLocation]+ # @param variants_scope [ActiveRecord::Relation]+ # @return [Spree::ServiceModule::Base::Result]def call(stock_location:, variants_scope: Spree::Variant)- prepared_stock_items = variants_scope.ids.map do |variant_id|- Hash[- 'stock_location_id', stock_location.id,- 'variant_id', variant_id,- 'backorderable', stock_location.backorderable_default,- 'created_at', Time.current,- 'updated_at', Time.current- ]- end- if prepared_stock_items.any?- stock_location.stock_items.insert_all(prepared_stock_items)- variants_scope.touch_all- end+ Spree::Deprecation.warn('Spree::StockLocations::StockItems::Create is deprecated and will be removed in Spree 6.1. Use Spree::StockLocations::StockLevels::Create instead.')+ Spree::StockLocations::StockLevels::Create.call(stock_location: stock_location, variants_scope: variants_scope)endendenddiff --git a/spree/core/app/services/spree/stock_locations/stock_levels/create.rb b/spree/core/app/services/spree/stock_locations/stock_levels/create.rbnew file mode 100644index 0000000000..1353bf06e0--- /dev/null+++ b/spree/core/app/services/spree/stock_locations/stock_levels/create.rb@@ -0,0 +1,25 @@+module Spree+ module StockLocations+ module StockLevels+ class Create+ prepend Spree::ServiceModule::Base++ def call(stock_location:, variants_scope: Spree::Variant)+ prepared_stock_levels = variants_scope.ids.map do |variant_id|+ Hash[+ 'stock_location_id', stock_location.id,+ 'variant_id', variant_id,+ 'backorderable', stock_location.backorderable_default,+ 'created_at', Time.current,+ 'updated_at', Time.current+ ]+ end+ if prepared_stock_levels.any?+ stock_location.stock_levels.insert_all(prepared_stock_levels)+ variants_scope.touch_all+ end+ end+ end+ end+ end+enddiff --git a/spree/core/app/services/spree/stock_reservations/reserve.rb b/spree/core/app/services/spree/stock_reservations/reserve.rbindex 4489c15845..1090ef62c2 100644--- a/spree/core/app/services/spree/stock_reservations/reserve.rb+++ b/spree/core/app/services/spree/stock_reservations/reserve.rb@@ -19,19 +19,19 @@ module Spree# Pessimistic lock + fresh read of count_on_hand. The lock serializes# concurrent checkouts and we use the locked rows below so we never# check stock against a stale association cache.- locked_stock_items = Spree::StockItem+ locked_stock_levels = Spree::StockLevel.where(id: targets.map { |_, si| si.id }).lock.index_by(&:id)- held = held_by_others(locked_stock_items.keys, cart)+ held = held_by_others(locked_stock_levels.keys, cart)existing = existing_reservations_for(targets)this_order_used = Hash.new(0)- targets.each do |line_item, stock_item|- stock_item = locked_stock_items.fetch(stock_item.id)- available = stock_item.count_on_hand - held.fetch(stock_item.id, 0) - this_order_used[stock_item.id]+ targets.each do |line_item, stock_level|+ stock_level = locked_stock_levels.fetch(stock_level.id)+ available = stock_level.count_on_hand - held.fetch(stock_level.id, 0) - this_order_used[stock_level.id]if available < line_item.quantityraise InsufficientStockError.new(@@ -45,10 +45,10 @@ module Spree)end- this_order_used[stock_item.id] += line_item.quantity+ this_order_used[stock_level.id] += line_item.quantity- reservation = existing[[stock_item.id, line_item.id]] ||- Spree::StockReservation.new(stock_item: stock_item, line_item: line_item)+ reservation = existing[[stock_level.id, line_item.id]] ||+ Spree::StockReservation.new(stock_level: stock_level, line_item: line_item)if cart.is_a?(Spree::Cart)reservation.cart = cartreservation.order = nil@@ -70,47 +70,47 @@ module Spreeprivatedef build_targets(cart)- cart.line_items.includes(variant: { stock_items: :stock_location }).filter_map do |line_item|+ cart.line_items.includes(variant: { stock_levels: :stock_location }).filter_map do |line_item|variant = line_item.variantnext unless variant&.should_track_inventory?- stock_item = select_stock_item(variant)+ stock_level = select_stock_level(variant)# Backorderable and pre-cart variants oversell past count_on_hand,# so a reservation capped at on-hand stock would wrongly reject them;# their cap is enforced by Stock::Quantifier#can_supply? instead.- next if stock_item.nil? || stock_item.backorderable? || variant.preorder?+ next if stock_level.nil? || stock_level.backorderable? || variant.preorder?- [line_item, stock_item]+ [line_item, stock_level]endend- def select_stock_item(variant)- variant.stock_items.detect { |si| si.stock_location&.active? && si.available? }+ def select_stock_level(variant)+ variant.stock_levels.detect { |si| si.stock_location&.active? && si.available? }end- def held_by_others(stock_item_ids, owner)- return {} if stock_item_ids.empty?+ def held_by_others(stock_level_ids, owner)+ return {} if stock_level_ids.empty?owner_column = owner.is_a?(Spree::Cart) ? :cart_id : :order_idSpree::StockReservation.active- .where(stock_item_id: stock_item_ids)+ .where(stock_level_id: stock_level_ids).where.not(owner_column => owner.id)- .group(:stock_item_id)+ .group(:stock_level_id).sum(:quantity)end- # One SELECT for all (stock_item_id, line_item_id) pairs we need to- # upsert. Returns a hash keyed by [stock_item_id, line_item_id].+ # One SELECT for all (stock_level_id, line_item_id) pairs we need to+ # upsert. Returns a hash keyed by [stock_level_id, line_item_id].def existing_reservations_for(targets)return {} if targets.empty?- stock_item_ids = targets.map { |_, si| si.id }+ stock_level_ids = targets.map { |_, si| si.id }line_item_ids = targets.map { |li, _| li.id }Spree::StockReservation- .where(stock_item_id: stock_item_ids, line_item_id: line_item_ids)- .index_by { |r| [r.stock_item_id, r.line_item_id] }+ .where(stock_level_id: stock_level_ids, line_item_id: line_item_ids)+ .index_by { |r| [r.stock_level_id, r.line_item_id] }endendenddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex d7a518bc94..a03ad6a456 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -492,9 +492,9 @@ en:spree/state:one: Stateother: States- spree/stock_item:- one: Stock Item- other: Stock Items+ spree/stock_level:+ one: Stock Level+ other: Stock Levelsspree/stock_location:one: Stock Locationother: Stock Locations@@ -1551,7 +1551,7 @@ en:states_required: States Requiredstatus: Statusstock: Stock- stock_items: Stock Items+ stock_levels: Stock Levelsstock_location: Stock Locationstock_locations: Stock Locationsstock_movements: Stock Movementsdiff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rbindex 18ab7632db..6b6336101e 100644--- a/spree/core/lib/spree/core.rb+++ b/spree/core/lib/spree/core.rb@@ -118,7 +118,7 @@ module Spreevariants: :default,categories: :default,collections: :default,- stock_location_stock_items: :default,+ stock_location_stock_levels: :default,coupon_codes: :default,themes: :default,addresses: :default,diff --git a/spree/core/lib/spree/core/controller_helpers/order.rb b/spree/core/lib/spree/core/controller_helpers/order.rbindex eb379089b5..db530f0c11 100644--- a/spree/core/lib/spree/core/controller_helpers/order.rb+++ b/spree/core/lib/spree/core/controller_helpers/order.rb@@ -166,10 +166,10 @@ module Spreevariant_includes = [:images,:prices,- :stock_items,+ :stock_levels,:stock_locations,{ option_values: :option_type },- { stock_items: :stock_location },+ { stock_levels: :stock_location },{ product: :default_variant }]diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rbindex 637eea7bbf..bc6bc95248 100644--- a/spree/core/lib/spree/core/engine.rb+++ b/spree/core/lib/spree/core/engine.rb@@ -447,7 +447,7 @@ module SpreeSpree::Return,Spree::Fulfillment,Spree::DeliveryMethod,- Spree::StockItem,+ Spree::StockLevel,Spree::StockTransfer,Spree::Store,Spree::StoreCredit,diff --git a/spree/core/lib/spree/core/permission_configuration.rb b/spree/core/lib/spree/core/permission_configuration.rbindex 46f1c5cdd1..0a3c2f8ef0 100644--- a/spree/core/lib/spree/core/permission_configuration.rb+++ b/spree/core/lib/spree/core/permission_configuration.rb@@ -302,7 +302,7 @@ module Spree[Spree::Collection, Spree::ProductCollection, Spree::CollectionRule]})register_resource(:stock, group: :catalog, audiences: %i[vendor], subjects: -> {- [Spree::StockItem, Spree::StockLocation, Spree::StockMovement,+ [Spree::StockLevel, Spree::StockLocation, Spree::StockMovement,Spree::StockTransfer, Spree::StockReservation]})diff --git a/spree/core/lib/spree/permitted_attributes.rb b/spree/core/lib/spree/permitted_attributes.rbindex c1fabf1d72..6189789e26 100644--- a/spree/core/lib/spree/permitted_attributes.rb+++ b/spree/core/lib/spree/permitted_attributes.rb@@ -55,7 +55,7 @@ module Spree:shipping_method_attributes,:channel_attributes,:source_attributes,- :stock_item_attributes,+ :stock_level_attributes,:stock_location_attributes,:stock_movement_attributes,:stock_transfer_attributes,@@ -229,7 +229,7 @@ module Spree:gateway_payment_profile_id, :last_digits, :name, :encrypted_data]- @@stock_item_attributes = [:variant_id, :stock_location_id, :backorderable, :count_on_hand, { metadata: {} }]+ @@stock_level_attributes = [:variant_id, :stock_location_id, :backorderable, :count_on_hand, { metadata: {} }]@@stock_location_attributes = [:name, :admin_name, :active, :address1, :address2, :city, :zipcode, :company,@@ -238,11 +238,11 @@ module Spree]@@stock_movement_attributes = [- :quantity, :stock_item, :stock_item_id, :originator, :action+ :quantity, :stock_level, :stock_level_id, :originator, :action]@@stock_transfer_attributes = [:source_location_id, :destination_location_id, :reference,- stock_movements_attributes: [:variant_id, :quantity, :originator_id, :stock_item_id]]+ stock_movements_attributes: [:variant_id, :quantity, :originator_id, :stock_level_id]]@@store_attributes = [:name, :url, :seo_title, :code, :meta_keywords,:meta_description, :default_currency, :default_country_code, :mail_from_address,@@ -286,7 +286,7 @@ module Spree:hs_code, :country_of_origin, :customs_description,{options: [:id, :name, :option_value_presentation, :option_value_name, :position, :_destroy],- stock_items_attributes: [:id, :count_on_hand, :stock_location_id, :backorderable, :_destroy],+ stock_levels_attributes: [:id, :count_on_hand, :stock_location_id, :backorderable, :_destroy],prices_attributes: [:id, :amount, :compare_at_amount, :currency, :_destroy],option_value_variants_attributes: [:id, :option_value_id, :_destroy],option_value_ids: []diff --git a/spree/core/lib/spree/testing_support/factories/order_factory.rb b/spree/core/lib/spree/testing_support/factories/order_factory.rbindex 1f5723830d..f5af129a2b 100644--- a/spree/core/lib/spree/testing_support/factories/order_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/order_factory.rb@@ -77,7 +77,7 @@ FactoryBot.define doorder.line_items.reloadend- stock_location = order.line_items&.first&.variant&.stock_items&.first&.stock_location || create(:stock_location)+ stock_location = order.line_items&.first&.variant&.stock_levels&.first&.stock_location || create(:stock_location)create(:shipment, order: order, cost: evaluator.shipment_cost, stock_location: stock_location)order.fulfillments.reloaddiff --git a/spree/core/lib/spree/testing_support/factories/product_factory.rb b/spree/core/lib/spree/testing_support/factories/product_factory.rbindex a01aadd9f6..fa342f4333 100644--- a/spree/core/lib/spree/testing_support/factories/product_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/product_factory.rb@@ -44,7 +44,7 @@ FactoryBot.define docreate(:stock_location) unless Spree::StockLocation.any?endafter(:create) do |product, evaluator|- existing_location_ids = product.default_variant.stock_items.pluck(:stock_location_id)+ existing_location_ids = product.default_variant.stock_levels.pluck(:stock_location_id)Spree::StockLocation.where.not(id: existing_location_ids).find_each do |stock_location|stock_location.propagate_variant(product.default_variant)end@@ -81,12 +81,12 @@ FactoryBot.define dofactory :product_in_stock doafter :create do |product|- product.default_variant.stock_items.first.adjust_count_on_hand(10)+ product.default_variant.stock_levels.first.adjust_count_on_hand(10)endtrait :without_backorder doafter :create do |product|- product.default_variant.stock_items.update_all(backorderable: false)+ product.default_variant.stock_levels.update_all(backorderable: false)endendenddiff --git a/spree/core/lib/spree/testing_support/factories/stock_location_factory.rb b/spree/core/lib/spree/testing_support/factories/stock_location_factory.rbindex e4204df962..9dc9dcc695 100644--- a/spree/core/lib/spree/testing_support/factories/stock_location_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/stock_location_factory.rb@@ -28,8 +28,8 @@ FactoryBot.define doproduct_1 = create(:product)product_2 = create(:product)- stock_location.stock_item_or_create(product_1.default_variant).adjust_count_on_hand(10)- stock_location.stock_item_or_create(product_2.default_variant).adjust_count_on_hand(20)+ stock_location.stock_level_or_create(product_1.default_variant).adjust_count_on_hand(10)+ stock_location.stock_level_or_create(product_2.default_variant).adjust_count_on_hand(20)endendenddiff --git a/spree/core/lib/spree/testing_support/factories/stock_movement_factory.rb b/spree/core/lib/spree/testing_support/factories/stock_movement_factory.rbindex decf7533eb..f3ac9c1b34 100644--- a/spree/core/lib/spree/testing_support/factories/stock_movement_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/stock_movement_factory.rb@@ -2,7 +2,7 @@ FactoryBot.define dofactory :stock_movement, class: Spree::StockMovement doquantity { 1 }action { 'sold' }- stock_item+ stock_levelendtrait :received dodiff --git a/spree/core/lib/spree/testing_support/factories/stock_reservation_factory.rb b/spree/core/lib/spree/testing_support/factories/stock_reservation_factory.rbindex 2d76c75f0c..2231983b6d 100644--- a/spree/core/lib/spree/testing_support/factories/stock_reservation_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/stock_reservation_factory.rb@@ -8,8 +8,8 @@ FactoryBot.define doend# Build the order first (with at least one line_item), then derive- # stock_item from that line_item's variant so the three FKs reference the- # same variant. Callers can override stock_item:/line_item:/order: to wire+ # stock_level from that line_item's variant so the three FKs reference the+ # same variant. Callers can override stock_level:/line_item:/order: to wire# up a specific scenario.after(:build) do |reservation, evaluator|if reservation.owner.nil?@@ -23,8 +23,8 @@ FactoryBot.define doowner.line_items.reloadend- reservation.stock_item ||= reservation.line_item.variant.stock_items.first ||- create(:stock_item, variant: reservation.line_item.variant)+ reservation.stock_level ||= reservation.line_item.variant.stock_levels.first ||+ create(:stock_level, variant: reservation.line_item.variant)endtrait :expired dodiff --git a/spree/core/lib/spree/testing_support/factories/variant_factory.rb b/spree/core/lib/spree/testing_support/factories/variant_factory.rbindex 82904f6eef..4f4ebea42d 100644--- a/spree/core/lib/spree/testing_support/factories/variant_factory.rb+++ b/spree/core/lib/spree/testing_support/factories/variant_factory.rb@@ -28,7 +28,7 @@ FactoryBot.define doafter(:create) do |variant, evaluator|if evaluator.create_stock- existing_location_ids = variant.stock_items.pluck(:stock_location_id)+ existing_location_ids = variant.stock_levels.pluck(:stock_location_id)Spree::StockLocation.where.not(id: existing_location_ids).find_each do |stock_location|stock_location.propagate_variant(variant)enddiff --git a/spree/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rb b/spree/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rbindex e85690cdfa..579e5e8362 100644--- a/spree/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rb+++ b/spree/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rb@@ -3,11 +3,26 @@ require 'spec_helper'module Spreedescribe StockLocations::StockItems::CreateJob, :job dolet!(:stock_location) { create :stock_location }+ let!(:variant) { create :variant }- it 'enqueues the creation of the stock location stock items' do+ # Subclassing rather than aliasing is what lets a job enqueued under the old+ # class name before the deploy still deserialize and run.+ it 'is a subclass of the renamed job' do+ expect(described_class.superclass).to eq(Spree::StockLocations::StockLevels::CreateJob)+ end++ it 'inherits the renamed queue' doexpect { described_class.perform_later(stock_location) }.to(- have_enqueued_job.on_queue(Spree.queues.stock_location_stock_items)+ have_enqueued_job.on_queue(Spree.queues.stock_location_stock_levels))end++ it 'warns and still does the work when performed' do+ expect(Spree::Deprecation).to receive(:warn).with(/StockLevels::CreateJob/)+ stock_location.stock_levels.unscope(:where).delete_all++ expect { described_class.perform_now(stock_location) }.+ to change { stock_location.stock_levels.count }.from(0)+ endendenddiff --git a/spree/core/spec/jobs/spree/stock_locations/stock_levels/create_job_spec.rb b/spree/core/spec/jobs/spree/stock_locations/stock_levels/create_job_spec.rbnew file mode 100644index 0000000000..b0b005d663--- /dev/null+++ b/spree/core/spec/jobs/spree/stock_locations/stock_levels/create_job_spec.rb@@ -0,0 +1,13 @@+require 'spec_helper'++module Spree+ describe StockLocations::StockLevels::CreateJob, :job do+ let!(:stock_location) { create :stock_location }++ it 'enqueues the creation of the stock location stock levels' do+ expect { described_class.perform_later(stock_location) }.to(+ have_enqueued_job.on_queue(Spree.queues.stock_location_stock_levels)+ )+ end+ end+enddiff --git a/spree/core/spec/models/spree/cart_spec.rb b/spree/core/spec/models/spree/cart_spec.rbindex ed4fd9b91e..36683cd486 100644--- a/spree/core/spec/models/spree/cart_spec.rb+++ b/spree/core/spec/models/spree/cart_spec.rb@@ -198,7 +198,7 @@ describe Spree::Cart, type: :model docontext 'when an item is out of stock' dobefore do- cart.line_items.first.variant.stock_items.update_all(count_on_hand: 0, backorderable: false)+ cart.line_items.first.variant.stock_levels.update_all(count_on_hand: 0, backorderable: false)endit 'removes the out of stock item' dodiff --git a/spree/core/spec/models/spree/event_spec.rb b/spree/core/spec/models/spree/event_spec.rbindex 1d4d55eb8b..323eec5a20 100644--- a/spree/core/spec/models/spree/event_spec.rb+++ b/spree/core/spec/models/spree/event_spec.rb@@ -132,8 +132,8 @@ RSpec.describe Spree::Event doendit 'handles nested event names' do- event = described_class.new(name: 'stock_item.low_stock', payload: {})- expect(event.resource_type).to eq('stock_item')+ event = described_class.new(name: 'stock_level.low_stock', payload: {})+ expect(event.resource_type).to eq('stock_level')endend@@ -144,7 +144,7 @@ RSpec.describe Spree::Event doendit 'handles multi-part actions' do- event = described_class.new(name: 'stock_item.low_stock', payload: {})+ event = described_class.new(name: 'stock_level.low_stock', payload: {})expect(event.action).to eq('low_stock')endenddiff --git a/spree/core/spec/models/spree/fulfilment_changer_spec.rb b/spree/core/spec/models/spree/fulfilment_changer_spec.rbindex a99af6674a..97dbbde5a3 100644--- a/spree/core/spec/models/spree/fulfilment_changer_spec.rb+++ b/spree/core/spec/models/spree/fulfilment_changer_spec.rb@@ -35,7 +35,7 @@ describe Spree::FulfilmentChanger dobefore doorder && desired_shipment- variant.stock_items.first.update_column(:count_on_hand, 100)+ variant.stock_levels.first.update_column(:count_on_hand, 100)endcontext 'when the order has multiple line items' do@@ -113,11 +113,11 @@ describe Spree::FulfilmentChanger docontext 'when transferring to another stock location' dolet(:desired_stock_location) { create(:stock_location) }- let!(:stock_item) { desired_stock_location.stock_item_or_create(variant) }+ let!(:stock_level) { desired_stock_location.stock_level_or_create(variant) }before do- stock_item.set_count_on_hand(desired_count_on_hand)- stock_item.update(backorderable: false)+ stock_level.set_count_on_hand(desired_count_on_hand)+ stock_level.update(backorderable: false)endcontext 'when the other stock location has enough stock' do@@ -145,7 +145,7 @@ describe Spree::FulfilmentChanger doendit 'does not unstock the desired location' do- expect { subject }.not_to change(stock_item, :count_on_hand)+ expect { subject }.not_to change(stock_level, :count_on_hand)endendend@@ -156,7 +156,7 @@ describe Spree::FulfilmentChanger dolet(:desired_count_on_hand) { 5 }before do- stock_item.update(backorderable: true)+ stock_level.update(backorderable: true)endit 'restocks seven at the original stock location' do@@ -241,21 +241,21 @@ describe Spree::FulfilmentChanger doendcontext 'when the original shipment had some backordered units' do- let(:current_stock_item) { current_shipment.stock_location.stock_item_or_create(variant) }- let(:desired_stock_item) { desired_shipment.stock_location.stock_items.find_by(variant: variant) }+ let(:current_stock_level) { current_shipment.stock_location.stock_level_or_create(variant) }+ let(:desired_stock_level) { desired_shipment.stock_location.stock_levels.find_by(variant: variant) }let(:backordered_units) { 6 }before docurrent_shipment.inventory_units.limit(backordered_units).update_all(state: :backordered)- current_stock_item.set_count_on_hand(-backordered_units)+ current_stock_level.set_count_on_hand(-backordered_units)endit 'restocks four at the original stock location' do- expect { subject }.to change { current_stock_item.reload.count_on_hand }.from(-backordered_units).to(1)+ expect { subject }.to change { current_stock_level.reload.count_on_hand }.from(-backordered_units).to(1)endit 'unstocks five at the desired stock location' do- expect { subject }.to change { desired_stock_item.reload.count_on_hand }.from(5).to(-2)+ expect { subject }.to change { desired_stock_level.reload.count_on_hand }.from(5).to(-2)endit 'creates a shipment with the correct number of on hand and backordered units' do@@ -404,9 +404,9 @@ describe Spree::FulfilmentChanger doendend- context 'when stock_item is last on_hand' do+ context 'when stock_level is last on_hand' dobefore do- variant.stock_items.first.update_column(:count_on_hand, 1)+ variant.stock_levels.first.update_column(:count_on_hand, 1)endlet(:current_shipment_inventory_unit_count) { 1 }diff --git a/spree/core/spec/models/spree/inventory_unit_spec.rb b/spree/core/spec/models/spree/inventory_unit_spec.rbindex 7c7b3f66cd..08956214f9 100644--- a/spree/core/spec/models/spree/inventory_unit_spec.rb+++ b/spree/core/spec/models/spree/inventory_unit_spec.rb@@ -2,7 +2,7 @@ require 'spec_helper'describe Spree::InventoryUnit, type: :model dolet(:stock_location) { create(:stock_location_with_items) }- let(:stock_item) { stock_location.stock_items.order(:id).first }+ let(:stock_level) { stock_location.stock_levels.order(:id).first }describe 'scopes' dolet!(:inventory_unit_1) { create(:inventory_unit, state: 'on_hand') }@@ -31,13 +31,13 @@ describe Spree::InventoryUnit, type: :model doendend- describe '#backordered_for_stock_item' do+ describe '#backordered_for_stock_level' dolet(:order) doorder = create(:order, state: 'complete', ship_address: create(:ship_address))order.completed_at = Time.currentcreate(:shipment, order: order, stock_location: stock_location)order.shipments.reload- create(:line_item, order: order, variant: stock_item.variant)+ create(:line_item, order: order, variant: stock_level.variant)order.line_items.reloadorder.tap(&:save!)end@@ -57,17 +57,17 @@ describe Spree::InventoryUnit, type: :model doendbefore do- stock_item.set_count_on_hand(-2)+ stock_level.set_count_on_hand(-2)end# Regression for #3066it 'returns modifiable objects' do- units = Spree::InventoryUnit.backordered_for_stock_item(stock_item)+ units = Spree::InventoryUnit.backordered_for_stock_level(stock_level)expect { units.first.save! }.not_to raise_errorendit "finds inventory units from its stock location when the unit's variant matches the stock item's variant" do- expect(Spree::InventoryUnit.backordered_for_stock_item(stock_item)).to match_array([unit])+ expect(Spree::InventoryUnit.backordered_for_stock_level(stock_level)).to match_array([unit])endit "does not find inventory units that aren't backordered" do@@ -76,7 +76,7 @@ describe Spree::InventoryUnit, type: :model doon_hand_unit.variant_id = 1on_hand_unit.save!- expect(Spree::InventoryUnit.backordered_for_stock_item(stock_item)).not_to include(on_hand_unit)+ expect(Spree::InventoryUnit.backordered_for_stock_level(stock_level)).not_to include(on_hand_unit)endit "does not find inventory units that don't match the stock item's variant" do@@ -85,13 +85,13 @@ describe Spree::InventoryUnit, type: :model doother_variant_unit.variant = create(:variant)other_variant_unit.save!- expect(Spree::InventoryUnit.backordered_for_stock_item(stock_item)).not_to include(other_variant_unit)+ expect(Spree::InventoryUnit.backordered_for_stock_level(stock_level)).not_to include(other_variant_unit)endit 'does not change shipping cost when fulfilling the order' docurrent_shipment_cost = shipment.costshipping_method.calculator.set_preference(:amount, current_shipment_cost + 5.0)- stock_item.set_count_on_hand(0)+ stock_level.set_count_on_hand(0)expect(shipment.reload.cost).to eq(current_shipment_cost)end@@ -116,13 +116,13 @@ describe Spree::InventoryUnit, type: :model dolet!(:other_unit) dounit = other_shipment.inventory_units.buildunit.state = 'backordered'- unit.variant_id = stock_item.variant.id+ unit.variant_id = stock_level.variant.idunit.order_id = other_order.idunit.tap(&:save!)endit 'does not find inventory units belonging to incomplete orders' do- expect(Spree::InventoryUnit.backordered_for_stock_item(stock_item)).not_to include(other_unit)+ expect(Spree::InventoryUnit.backordered_for_stock_level(stock_level)).not_to include(other_unit)endendenddiff --git a/spree/core/spec/models/spree/line_item_spec.rb b/spree/core/spec/models/spree/line_item_spec.rbindex d2142d3f2f..0c7b3d9acf 100644--- a/spree/core/spec/models/spree/line_item_spec.rb+++ b/spree/core/spec/models/spree/line_item_spec.rb@@ -167,12 +167,12 @@ describe Spree::LineItem, type: :model doit 'decrements stock for a tracked digital variant' dosecond_licence = create(:product, delivery_profile: digital_profile, store: store).default_variantsecond_licence.update!(track_inventory: true)- stock_item = second_licence.stock_items.first- stock_item.set_count_on_hand(5)+ stock_level = second_licence.stock_levels.first+ stock_level.set_count_on_hand(5)expect {create(:line_item, order: order, variant: second_licence, quantity: 2)- }.to change { stock_item.reload.count_on_hand }.from(5).to(3)+ }.to change { stock_level.reload.count_on_hand }.from(5).to(3)endend@@ -411,7 +411,7 @@ describe Spree::LineItem, type: :model docontext 'nothing left on stock' dobefore do- variant.stock_items.update_all count_on_hand: 5, backorderable: false+ variant.stock_levels.update_all count_on_hand: 5, backorderable: falseSpree::Orders::AddItem.call(order: order, variant: variant, quantity: 5)order.rebuild_fulfillments!order.finalize!@@ -440,7 +440,7 @@ describe Spree::LineItem, type: :model docontext '2 items left on stock' dobefore do- variant.stock_items.update_all count_on_hand: 7, backorderable: false+ variant.stock_levels.update_all count_on_hand: 7, backorderable: falseSpree::Orders::AddItem.call(order: order, variant: variant, quantity: 5)order.rebuild_fulfillments!order.finalize!diff --git a/spree/core/spec/models/spree/order_inventory_spec.rb b/spree/core/spec/models/spree/order_inventory_spec.rbindex 52b6d39700..3743c9be9e 100644--- a/spree/core/spec/models/spree/order_inventory_spec.rb+++ b/spree/core/spec/models/spree/order_inventory_spec.rb@@ -47,7 +47,7 @@ describe Spree::OrderInventory, type: :model dobefore { stub_store_preferences(track_inventory_levels: false) }it 'creates only on hand inventory units' do- variant.stock_items.delete_all+ variant.stock_levels.delete_allexpect_any_instance_of(Spree::StockLocation).not_to receive(:unstock)@@ -66,7 +66,7 @@ describe Spree::OrderInventory, type: :model dobefore { variant.update(track_inventory: false) }it 'creates only on hand inventory units' do- variant.stock_items.delete_all+ variant.stock_levels.delete_allexpect_any_instance_of(Spree::StockLocation).not_to receive(:unstock)@@ -82,8 +82,8 @@ describe Spree::OrderInventory, type: :model doit 'creates stock_movement' doexpect(subject.send(:add_to_shipment, shipment, 5)).to eq(5)- stock_item = shipment.stock_location.stock_item(subject.variant)- movement = stock_item.stock_movements.last+ stock_level = shipment.stock_location.stock_level(subject.variant)+ movement = stock_level.stock_movements.last# movement.originator.should == shipmentexpect(movement.quantity).to eq(-5)end@@ -234,7 +234,7 @@ describe Spree::OrderInventory, type: :model doit 'doesn\'t create on_hand items from backordered items' doshipment.set_up_inventory('backordered', variant, order, line_item)- expect { subject.send(:remove_from_shipment, shipment, 3) }.to change { line_item.variant.stock_items.sum(:count_on_hand) }.from(-2).to(0)+ expect { subject.send(:remove_from_shipment, shipment, 3) }.to change { line_item.variant.stock_levels.sum(:count_on_hand) }.from(-2).to(0)endendenddiff --git a/spree/core/spec/models/spree/order_routing/rules/minimize_splits_spec.rb b/spree/core/spec/models/spree/order_routing/rules/minimize_splits_spec.rbindex 5cadeac420..ca8922e62c 100644--- a/spree/core/spec/models/spree/order_routing/rules/minimize_splits_spec.rb+++ b/spree/core/spec/models/spree/order_routing/rules/minimize_splits_spec.rb@@ -16,9 +16,9 @@ RSpec.describe Spree::OrderRouting::Rules::MinimizeSplits, type: :model dolet(:half_coverage) { create(:stock_location) }before do- full_coverage.stock_item_or_create(variant_a).update!(count_on_hand: 10)- full_coverage.stock_item_or_create(variant_b).update!(count_on_hand: 10)- half_coverage.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ full_coverage.stock_level_or_create(variant_a).update!(count_on_hand: 10)+ full_coverage.stock_level_or_create(variant_b).update!(count_on_hand: 10)+ half_coverage.stock_level_or_create(variant_a).update!(count_on_hand: 10)# variant_b: no stock item / 0 count at half_coverage so coverage is 1.enddiff --git a/spree/core/spec/models/spree/order_routing/strategy/parity_spec.rb b/spree/core/spec/models/spree/order_routing/strategy/parity_spec.rbindex ff5355eafa..9a2984c2da 100644--- a/spree/core/spec/models/spree/order_routing/strategy/parity_spec.rb+++ b/spree/core/spec/models/spree/order_routing/strategy/parity_spec.rb@@ -42,7 +42,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doo.reloadend- before { nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10) }+ before { nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10) }it 'allocates from NYC, no backorder' dounits = location_units(strategy.for_allocation)@@ -63,8 +63,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10)- nyc.stock_item_or_create(variant_b).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_b).update!(count_on_hand: 10)endit 'NYC covers everything, LA never engaged' do@@ -87,8 +87,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10)- la.stock_item_or_create(variant_b).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10)+ la.stock_level_or_create(variant_b).update!(count_on_hand: 10)endit 'splits variant_a → NYC, variant_b → LA' do@@ -111,9 +111,9 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10)- la.stock_item_or_create(variant_b).update!(count_on_hand: 10)- berlin.stock_item_or_create(variant_c).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10)+ la.stock_level_or_create(variant_b).update!(count_on_hand: 10)+ berlin.stock_level_or_create(variant_c).update!(count_on_hand: 10)endit 'splits across all three' do@@ -136,8 +136,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 2)- la.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 2)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 10)end# Coordinator (Legacy) packs every location and splits NYC: 2 / LA: 3.@@ -160,8 +160,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 5)- la.stock_item_or_create(variant_a).update!(count_on_hand: 5)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 5)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 5)endit 'must split across both locations to avoid backorder' do@@ -183,8 +183,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 2, backorderable: true)- la.stock_item_or_create(variant_a).update!(count_on_hand: 3, backorderable: true)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 2, backorderable: true)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 3, backorderable: true)endit 'allocates available stock plus backorders the rest' do@@ -207,9 +207,9 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10)- nyc.stock_item_or_create(variant_b).update!(count_on_hand: 1)- la.stock_item_or_create(variant_b).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_b).update!(count_on_hand: 1)+ la.stock_level_or_create(variant_b).update!(count_on_hand: 10)endit 'allocates 2 of variant_a and 1 of variant_b at NYC, 3 of variant_b at LA' do@@ -231,7 +231,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)endit 'returns a single backordered package' do@@ -241,7 +241,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendend- context 'variant has stock_items but zero count, backorderable everywhere' do+ context 'variant has stock_levels but zero count, backorderable everywhere' dolet(:variant_unstocked) { create(:variant) }let(:order) do@@ -251,7 +251,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- Spree::StockItem.where(variant_id: variant_unstocked.id).update_all(backorderable: true)+ Spree::StockLevel.where(variant_id: variant_unstocked.id).update_all(backorderable: true)endit 'returns a single backordered package' do@@ -305,13 +305,13 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 1)- la.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 1)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 10)# Other cart reserves the only NYC unit.- stock_item = nyc.stock_item(variant_a)+ stock_level = nyc.stock_level(variant_a)create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_order.line_items.first,order: other_order,quantity: 1,@@ -336,12 +336,12 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 5)- la.stock_item_or_create(variant_a).update!(count_on_hand: 5)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 5)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 5)- stock_item = nyc.stock_item(variant_a)+ stock_level = nyc.stock_level(variant_a)create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: order.line_items.first,order: order,quantity: 1,@@ -369,12 +369,12 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 1)- la.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 1)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 10)- stock_item = nyc.stock_item(variant_a)+ stock_level = nyc.stock_level(variant_a)create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_order.line_items.first,order: other_order,quantity: 1,@@ -398,7 +398,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doo.reloadend- before { nyc.stock_item_or_create(variant_a).update!(count_on_hand: 5) }+ before { nyc.stock_level_or_create(variant_a).update!(count_on_hand: 5) }it 'allocates unaffected by reservation feature flag' dopackages = strategy.for_allocation@@ -422,8 +422,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_shipping).update!(count_on_hand: 10)- nyc.stock_item_or_create(variant_digital).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_shipping).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_digital).update!(count_on_hand: 10)end# The DeliveryProfile splitter is in the default Spree.stock_splitters@@ -448,9 +448,9 @@ RSpec.describe 'OrderRouting strategy parity', type: :model docontext 'one location inactive, the other active' dobefore do# Mark NYC inactive after the variant's after(:create) hook has- # propagated stock_items to it.- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10)- la.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ # propagated stock_levels to it.+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 10)nyc.update!(active: false)end@@ -473,7 +473,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do# Asymmetric stocking: variant present at only one location# ---------------------------------------------------------------- context 'variant only stocked at LA, NYC has no stock_item for it' do+ context 'variant only stocked at LA, NYC has no stock_level for it' dolet(:order) doo = create(:order, store: store, ship_address: create(:ship_address))create(:line_item, order: o, variant: variant_a, quantity: 2)@@ -481,11 +481,11 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- # Drop the auto-propagated NYC stock_item entirely so NYC truly+ # Drop the auto-propagated NYC stock_level entirely so NYC truly# doesn't stock variant_a (Coordinator's eligible-locations join- # filters by stock_item presence; we want to exercise that path).- nyc.stock_items.where(variant_id: variant_a.id).destroy_all- la.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ # filters by stock_level presence; we want to exercise that path).+ nyc.stock_levels.where(variant_id: variant_a.id).destroy_all+ la.stock_level_or_create(variant_a).update!(count_on_hand: 10)endit 'routes everything to LA' do@@ -504,13 +504,13 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do# Stock available during line-item creation (so AvailabilityValidator# passes), then dropped to 0 + non-backorderable to model the# "no eligible inventory" routing scenario.- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10, backorderable: false)- la.stock_item_or_create(variant_a).update!(count_on_hand: 10, backorderable: false)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10, backorderable: false)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 10, backorderable: false)o = create(:order, store: store, ship_address: create(:ship_address))create(:line_item, order: o, variant: variant_a, quantity: 1)- nyc.stock_item(variant_a).update!(count_on_hand: 0)- la.stock_item(variant_a).update!(count_on_hand: 0)+ nyc.stock_level(variant_a).update!(count_on_hand: 0)+ la.stock_level(variant_a).update!(count_on_hand: 0)o.reloadend@@ -536,7 +536,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 10)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 10)# variant_b has no on_hand anywhere — but track_inventory: false# means Packer adds it directly without consulting fill_status.end@@ -563,8 +563,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 1, backorderable: false)- la.stock_item_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 1, backorderable: false)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)end# The Adjuster's update_backorder only shrinks a captured backorder@@ -597,8 +597,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model doendbefore do- nyc.stock_item_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)- la.stock_item_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)+ nyc.stock_level_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)+ la.stock_level_or_create(variant_a).update!(count_on_hand: 0, backorderable: true)endit 'backorders all units at a single location' dodiff --git a/spree/core/spec/models/spree/order_routing/strategy/rules_spec.rb b/spree/core/spec/models/spree/order_routing/strategy/rules_spec.rbindex effd00295a..f3bf5fa352 100644--- a/spree/core/spec/models/spree/order_routing/strategy/rules_spec.rb+++ b/spree/core/spec/models/spree/order_routing/strategy/rules_spec.rb@@ -17,7 +17,7 @@ RSpec.describe Spree::OrderRouting::Strategy::Rules, type: :model dobefore do[default_loc, preferred_loc].each do |loc|- loc.stock_item_or_create(variant).update!(count_on_hand: 10)+ loc.stock_level_or_create(variant).update!(count_on_hand: 10)endenddiff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rbindex 06605cfb69..f77085c2d3 100644--- a/spree/core/spec/models/spree/order_spec.rb+++ b/spree/core/spec/models/spree/order_spec.rb@@ -2064,13 +2064,13 @@ describe Spree::Order, type: :model dobefore docreate(:line_item, order: order, variant: variant, quantity: 1)- variant.stock_items.first.update(count_on_hand: 0, backorderable: true)+ variant.stock_levels.first.update(count_on_hand: 0, backorderable: true)create(:line_item, order: order, variant: variant_2, quantity: 1)- variant_2.stock_items.first.update(count_on_hand: 1, backorderable: true)+ variant_2.stock_levels.first.update(count_on_hand: 1, backorderable: true)create(:line_item, order: order, variant: variant_3, quantity: 1)- variant_3.stock_items.first.update(count_on_hand: 0, backorderable: true)+ variant_3.stock_levels.first.update(count_on_hand: 0, backorderable: true)endit 'returns the backordered variants' do@@ -2310,7 +2310,7 @@ describe Spree::Order, type: :model docontext 'when an item is out of stock' dobefore do- order.line_items.first.variant.stock_items.update_all(count_on_hand: 0, backorderable: false)+ order.line_items.first.variant.stock_levels.update_all(count_on_hand: 0, backorderable: false)endit 'removes the out of stock item' dodiff --git a/spree/core/spec/models/spree/product/scopes_spec.rb b/spree/core/spec/models/spree/product/scopes_spec.rbindex fc6aaf8ef8..f405df46e7 100644--- a/spree/core/spec/models/spree/product/scopes_spec.rb+++ b/spree/core/spec/models/spree/product/scopes_spec.rb@@ -197,19 +197,19 @@ describe 'Product scopes', type: :model dodescribe '#in_stock / #out_of_stock' dolet!(:in_stock_product) docreate(:product).tap do |p|- p.stock_items.update_all(count_on_hand: 10, backorderable: false)+ p.stock_levels.update_all(count_on_hand: 10, backorderable: false)endendlet!(:backorderable_product) docreate(:product).tap do |p|- p.stock_items.update_all(count_on_hand: 0, backorderable: true)+ p.stock_levels.update_all(count_on_hand: 0, backorderable: true)endendlet!(:out_of_stock_product) docreate(:product).tap do |p|- p.stock_items.update_all(count_on_hand: 0, backorderable: false)+ p.stock_levels.update_all(count_on_hand: 0, backorderable: false)endenddiff --git a/spree/core/spec/models/spree/product_preorder_spec.rb b/spree/core/spec/models/spree/product_preorder_spec.rbindex 306f566584..074814938b 100644--- a/spree/core/spec/models/spree/product_preorder_spec.rb+++ b/spree/core/spec/models/spree/product_preorder_spec.rb@@ -8,7 +8,7 @@ RSpec.describe 'Pre-order', type: :model dolet(:store) { @default_store }let(:product) { create(:product, status: 'active') }let(:variant) { create(:variant, product: product, price: 20) }- let(:stock_item) { variant.stock_items.first }+ let(:stock_level) { variant.stock_levels.first }def quantifierSpree::Stock::Quantifier.new(variant)@@ -51,7 +51,7 @@ RSpec.describe 'Pre-order', type: :model dodescribe 'pre-order does not bypass the product status' doit 'cannot supply an archived product even when the variant is preorderable' dovariant.update!(preorderable: true)- stock_item.set_count_on_hand(5)+ stock_level.set_count_on_hand(5)product.update!(status: 'archived')expect(quantifier.can_supply?(1)).to be falseend@@ -61,8 +61,8 @@ RSpec.describe 'Pre-order', type: :model do# variant may sell, whether the oversell is a backorder or a pre-order.describe 'backorder_limit' dobefore do- stock_item.update!(backorderable: false)- stock_item.set_count_on_hand(0)+ stock_level.update!(backorderable: false)+ stock_level.set_count_on_hand(0)endcontext 'pre-order with no backorder_limit (empty = unlimited)' do@@ -90,13 +90,13 @@ RSpec.describe 'Pre-order', type: :model doendit 'combines on-hand stock with the limit' do- stock_item.set_count_on_hand(2)+ stock_level.set_count_on_hand(2)expect(quantifier.can_supply?(7)).to be trueexpect(quantifier.can_supply?(8)).to be falseendit 'is not purchasable once the limit is used up' do- stock_item.set_count_on_hand(-5) # 5 units already oversold+ stock_level.set_count_on_hand(-5) # 5 units already oversoldexpect(quantifier.can_supply?(1)).to be falseexpect(variant.purchasable?).to be falseend@@ -104,7 +104,7 @@ RSpec.describe 'Pre-order', type: :model do# The limit is universal — it caps a plain backorder too, not just pre-orders.context 'plain backorder (not a pre-order)' do- before { stock_item.update!(backorderable: true) }+ before { stock_level.update!(backorderable: true) }it 'caps the backorder at the limit' dovariant.update!(backorder_limit: 3)@@ -141,12 +141,12 @@ RSpec.describe 'Pre-order', type: :model do# units, exactly like backorders, so process_backorders can fill them when# stock arrives.describe 'fulfillment (StockLocation#fill_status)' do- let(:stock_location) { stock_item.stock_location }+ let(:stock_location) { stock_level.stock_location }before dovariant.update!(preorderable: true, backorder_limit: 5)- stock_item.update!(backorderable: false)- stock_item.set_count_on_hand(2)+ stock_level.update!(backorderable: false)+ stock_level.set_count_on_hand(2)endit 'backorders the shortfall for a pre-order variant' do@@ -167,8 +167,8 @@ RSpec.describe 'Pre-order', type: :model dobefore dovariant.update!(preorderable: true, backorder_limit: 5)- stock_item.update!(backorderable: false)- stock_item.set_count_on_hand(0)+ stock_level.update!(backorderable: false)+ stock_level.set_count_on_hand(0)publication.update!(published_at: 2.months.from_now) # not yet publishedproduct.product_publications.resetenddiff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex 342469049e..ca44674bc3 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -54,7 +54,7 @@ describe Spree::Product, type: :model do# Baseline: put every variant out of stock and non-backorderable.def all_variants_unavailable(product)product.reload.variants.each do |v|- v.stock_items.update_all(count_on_hand: 0, backorderable: false)+ v.stock_levels.update_all(count_on_hand: 0, backorderable: false)endend@@ -72,9 +72,9 @@ describe Spree::Product, type: :model doall_variants_unavailable(product)if method_name == 'in_stock'- variant.stock_items.update_all(count_on_hand: 10)+ variant.stock_levels.update_all(count_on_hand: 10)else- variant.stock_items.update_all(backorderable: true)+ variant.stock_levels.update_all(backorderable: true)endexpect(product.reload.send("#{method_name}?")).to eq true@@ -244,7 +244,7 @@ describe Spree::Product, type: :model doendit 'is false' do- product.variants.each { |v| v.stock_items.update_all count_on_hand: 0, backorderable: false }+ product.variants.each { |v| v.stock_levels.update_all count_on_hand: 0, backorderable: false }expect(product.can_supply?).to be(false)endend@@ -262,10 +262,10 @@ describe Spree::Product, type: :model docontext 'has stock movements' dolet(:variant) { product.default_variant }- let(:stock_item) { variant.stock_items.first }+ let(:stock_level) { variant.stock_levels.first }it 'doesnt raise ReadOnlyRecord error' do- Spree::StockMovement.create!(stock_item: stock_item, quantity: 1)+ Spree::StockMovement.create!(stock_level: stock_level, quantity: 1)expect { product.destroy }.not_to raise_errorendend@@ -273,8 +273,8 @@ describe Spree::Product, type: :model do# Regression test for #3737context 'has stock items' doit 'can retrieve stock items' do- expect(product.default_variant.stock_items.first).not_to be_nil- expect(product.stock_items.first).not_to be_nil+ expect(product.default_variant.stock_levels.first).not_to be_nil+ expect(product.stock_levels.first).not_to be_nilendend@@ -335,7 +335,7 @@ describe Spree::Product, type: :model dolet(:incorrent_total_on_hand) { 15 }before do- product.stock_items.first.set_count_on_hand corrent_total_on_hand+ product.stock_levels.first.set_count_on_hand corrent_total_on_handproduct.instance_variable_set(:@total_on_hand, incorrent_total_on_hand)end@@ -462,9 +462,9 @@ describe Spree::Product, type: :model doit 'creates a default stock item' doproduct.saveexpect(product.default_variant.track_inventory?).to eq(false)- expect(product.default_variant.stock_items.count).to eq(1)- expect(product.default_variant.stock_items.first.count_on_hand).to eq(0)- expect(product.default_variant.stock_items.first.backorderable).to eq(false)+ expect(product.default_variant.stock_levels.count).to eq(1)+ expect(product.default_variant.stock_levels.first.count_on_hand).to eq(0)+ expect(product.default_variant.stock_levels.first.backorderable).to eq(false)endend@@ -687,13 +687,13 @@ describe Spree::Product, type: :model doendit 'returns sum of stock items count_on_hand' do- product.stock_items.first.set_count_on_hand 5+ product.stock_levels.first.set_count_on_hand 5product.variants.reload # force load associationexpect(product.total_on_hand).to be(5)endit 'returns sum of stock items count_on_hand when variants are not loaded' do- product.stock_items.first.set_count_on_hand 5+ product.stock_levels.first.set_count_on_hand 5expect(product.reload.total_on_hand).to be(5)endend@@ -751,12 +751,12 @@ describe Spree::Product, type: :model doendit 'returns false when out of stock and not backorderable' do- product.stock_items.first.update(backorderable: false)+ product.stock_levels.first.update(backorderable: false)expect(product.backordered?).to eq(false)endit 'returns false when there is available item in stock' do- product.stock_items.first.update(count_on_hand: 10)+ product.stock_levels.first.update(count_on_hand: 10)expect(product.backordered?).to eq(false)endend@@ -1236,7 +1236,7 @@ describe Spree::Product, type: :model dosubject { product.any_variant_in_stock_or_backorderable? }let!(:product) { create(:product) }- let(:stock_item) { variant.stock_items.first }+ let(:stock_level) { variant.stock_levels.first }context 'when only default variant is in stock or backorderable' doit { expect(subject).to eq(true) }@@ -1247,21 +1247,21 @@ describe Spree::Product, type: :model dolet!(:variant) { create(:variant, product: product) }before do- Spree::StockItem.update_all(backorderable: false)+ Spree::StockLevel.update_all(backorderable: false)product.reloadendcontext 'with at least one option variant stock items count_on_hand > 0' dobefore do# make all stock items in stock, also for the default variant- Spree::StockItem.all.each { |stock_item| stock_item.set_count_on_hand(1) }+ Spree::StockLevel.all.each { |stock_level| stock_level.set_count_on_hand(1) }endit { expect(subject).to eq(true) }endcontext 'when all option variant stock items have count_on_hand <= 0' do- before { stock_item.set_count_on_hand(0) }+ before { stock_level.set_count_on_hand(0) }it { expect(subject).to eq(false) }@@ -1275,7 +1275,7 @@ describe Spree::Product, type: :model doit { expect(subject).to eq(false) }context 'when all option variant stock items have backorderable = true' do- before { stock_item.update(backorderable: true) }+ before { stock_level.update(backorderable: true) }it { expect(subject).to eq(true) }end@@ -1287,9 +1287,9 @@ describe Spree::Product, type: :model dolet!(:variant) { create(:variant, product: product) }before do- Spree::StockItem.update_all(backorderable: false)- product.default_variant.stock_items.first.set_count_on_hand(0)- variant.stock_items.first.set_count_on_hand(10)+ Spree::StockLevel.update_all(backorderable: false)+ product.default_variant.stock_levels.first.set_count_on_hand(0)+ variant.stock_levels.first.set_count_on_hand(10)variant.update_column(:discontinue_on, 1.day.ago)product.reloadenddiff --git a/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rb b/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rbindex 29d5dd08b8..9962511dca 100644--- a/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rb+++ b/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rb@@ -9,8 +9,8 @@ describe Spree::Promotion::Actions::CreateLineItems, type: :model dolet(:payload) { { order: order } }def empty_stock(variant)- variant.stock_items.update_all(backorderable: false)- variant.stock_items.each(&:reduce_count_on_hand_to_zero)+ variant.stock_levels.update_all(backorderable: false)+ variant.stock_levels.each(&:reduce_count_on_hand_to_zero)endcontext '#perform' dodiff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rbindex 15e637ca1a..46c48b8f61 100644--- a/spree/core/spec/models/spree/shipment_spec.rb+++ b/spree/core/spec/models/spree/shipment_spec.rb@@ -1047,7 +1047,7 @@ describe Spree::Shipment, type: :model dolet(:variant) { order.line_items.first.variant }before do- perform_enqueued_jobs(only: Spree::StockLocations::StockItems::CreateJob)+ perform_enqueued_jobs(only: Spree::StockLocations::StockLevels::CreateJob)shipping_method = order.fulfillments.first.shipping_methodshipping_method.calculator.preferences[:amount] = order.fulfillments.first.costshipping_method.calculator.save!diff --git a/spree/core/spec/models/spree/stock/coordinator_spec.rb b/spree/core/spec/models/spree/stock/coordinator_spec.rbindex b8f3c0d895..198b8ed971 100644--- a/spree/core/spec/models/spree/stock/coordinator_spec.rb+++ b/spree/core/spec/models/spree/stock/coordinator_spec.rb@@ -85,7 +85,7 @@ module Spreelet!(:product) { create(:product) }let!(:order) do- product.stock_items.map { |stock_item| stock_item.adjust_count_on_hand(1) }+ product.stock_levels.map { |stock_level| stock_level.adjust_count_on_hand(1) }line_item = create(:line_item, product: product, variant: product.default_variant, quantity: 2)line_item.orderenddiff --git a/spree/core/spec/models/spree/stock/quantifier_spec.rb b/spree/core/spec/models/spree/stock/quantifier_spec.rbindex 569a85140a..3b42ec4a34 100644--- a/spree/core/spec/models/spree/stock/quantifier_spec.rb+++ b/spree/core/spec/models/spree/stock/quantifier_spec.rb@@ -11,19 +11,19 @@ endmodule Spreemodule Stockdescribe Quantifier, type: :model do- subject { described_class.new(stock_item.variant) }+ subject { described_class.new(stock_level.variant) }before(:all) { Spree::StockLocation.delete_all } # FIXME: leaky databaselet!(:stock_location) { create :stock_location_with_items }- let!(:stock_item) { stock_location.stock_items.order(:id).first }+ let!(:stock_level) { stock_location.stock_levels.order(:id).first }- specify { expect(subject.stock_items).to eq([stock_item]) }- specify { expect(subject.variant).to eq(stock_item.variant) }+ specify { expect(subject.stock_levels).to eq([stock_level]) }+ specify { expect(subject.variant).to eq(stock_level.variant) }context 'with a single stock location/item' do- it 'total_on_hand should match stock_item' do- expect(subject.total_on_hand).to eq(stock_item.count_on_hand)+ it 'total_on_hand should match stock_level' do+ expect(subject.total_on_hand).to eq(stock_level.count_on_hand)endcontext 'when variant is available' do@@ -40,7 +40,7 @@ module Spreeendcontext 'when variant inventory tracking is off' do- before { stock_item.variant.track_inventory = false }+ before { stock_level.variant.track_inventory = false }specify { expect(subject.total_on_hand).to eq(Float::INFINITY) }@@ -54,7 +54,7 @@ module Spreeendcontext 'when stock item prevents backordering' do- before { stock_item.update(backorderable: false) }+ before { stock_level.update(backorderable: false) }specify { expect(subject.backorderable?).to be false }@@ -80,12 +80,12 @@ module Spreelet!(:stock_location_3) { create :stock_location, active: false, propagate_all_variants: true }before do- perform_enqueued_jobs(only: Spree::StockLocations::StockItems::CreateJob)- stock_location_2.stock_items.where(variant_id: stock_item.variant).update_all(count_on_hand: 5, backorderable: false)- stock_location_3.stock_items.where(variant_id: stock_item.variant).update_all(count_on_hand: 5, backorderable: false)+ perform_enqueued_jobs(only: Spree::StockLocations::StockLevels::CreateJob)+ stock_location_2.stock_levels.where(variant_id: stock_level.variant).update_all(count_on_hand: 5, backorderable: false)+ stock_location_3.stock_levels.where(variant_id: stock_level.variant).update_all(count_on_hand: 5, backorderable: false)end- it 'total_on_hand should total all active stock_items' do+ it 'total_on_hand should total all active stock_levels' doexpect(subject.total_on_hand).to eq(15)end@@ -101,7 +101,7 @@ module Spreeendcontext 'when all stock items prevent backordering' do- before { stock_item.update(backorderable: false) }+ before { stock_level.update(backorderable: false) }specify { expect(subject.backorderable?).to be false }@@ -123,10 +123,10 @@ module Spreeendcontext 'with active stock reservations' do- before { stock_item.update(backorderable: false) }+ before { stock_level.update(backorderable: false) }let(:other_order) { create(:order) }- let(:other_line_item) { create(:line_item, order: other_order, variant: stock_item.variant) }+ let(:other_line_item) { create(:line_item, order: other_order, variant: stock_level.variant) }context 'when stock_reservations_enabled is true' dobefore { stub_store_preferences(stock_reservations_enabled: true) }@@ -134,32 +134,32 @@ module Spreeit 'subtracts active reservations from total_on_hand' docreate(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 4,expires_at: 5.minutes.from_now)- expect(subject.total_on_hand).to eq(stock_item.count_on_hand - 4)+ expect(subject.total_on_hand).to eq(stock_level.count_on_hand - 4)endit 'ignores expired reservations' docreate(:stock_reservation,:expired,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 4)- expect(subject.total_on_hand).to eq(stock_item.count_on_hand)+ expect(subject.total_on_hand).to eq(stock_level.count_on_hand)endit 'clamps total_on_hand at zero when reservations exceed physical stock' do- stock_item.set_count_on_hand(2)+ stock_level.set_count_on_hand(2)create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 5,@@ -171,7 +171,7 @@ module Spreeit 'reserved_quantity returns the sum of active reservations' docreate(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 3,@@ -181,28 +181,28 @@ module Spreeendcontext 'with an excluded order' do- subject { described_class.new(stock_item.variant, excluded_order: own_order) }+ subject { described_class.new(stock_level.variant, excluded_order: own_order) }let(:own_order) { create(:order) }- let(:own_line_item) { create(:line_item, order: own_order, variant: stock_item.variant) }+ let(:own_line_item) { create(:line_item, order: own_order, variant: stock_level.variant) }it "does not count the excluded order's own reservations" docreate(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: own_line_item,order: own_order,quantity: 4,expires_at: 5.minutes.from_now)expect(subject.reserved_quantity).to eq(0)- expect(subject.total_on_hand).to eq(stock_item.count_on_hand)+ expect(subject.total_on_hand).to eq(stock_level.count_on_hand)endit "still counts other orders' reservations" docreate(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: own_line_item,order: own_order,quantity: 4,@@ -210,7 +210,7 @@ module Spree)create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 2,@@ -220,12 +220,12 @@ module Spreeendcontext 'when the excluded order is not yet persisted' do- subject { described_class.new(stock_item.variant, excluded_order: Spree::Order.new) }+ subject { described_class.new(stock_level.variant, excluded_order: Spree::Order.new) }it 'counts all reservations rather than excluding everything' docreate(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 2,@@ -243,13 +243,13 @@ module Spreeit 'returns raw count_on_hand even when reservations exist' docreate(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 4,expires_at: 5.minutes.from_now)- expect(subject.total_on_hand).to eq(stock_item.count_on_hand)+ expect(subject.total_on_hand).to eq(stock_level.count_on_hand)expect(subject.reserved_quantity).to eq(0)endenddiff --git a/spree/core/spec/models/spree/stock_level_spec.rb b/spree/core/spec/models/spree/stock_level_spec.rbindex 94cdc6aee6..e91a94105b 100644--- a/spree/core/spec/models/spree/stock_level_spec.rb+++ b/spree/core/spec/models/spree/stock_level_spec.rb@@ -1,7 +1,7 @@require 'spec_helper'describe Spree::StockLevel, type: :model do- subject { stock_location.stock_items.order(:id).first }+ subject { stock_location.stock_levels.order(:id).first }let(:stock_location) { create(:stock_location_with_items) }@@ -273,15 +273,15 @@ describe Spree::StockLevel, type: :model doit 'recreates stock item just fine' doexpect do- stock_location.stock_items.create!(variant: subject.variant)+ stock_location.stock_levels.create!(variant: subject.variant)end.not_to raise_errorendit 'doesnt allow recreating more than one stock item at once' do- stock_location.stock_items.create!(variant: subject.variant)+ stock_location.stock_levels.create!(variant: subject.variant)expect do- stock_location.stock_items.create!(variant: subject.variant)+ stock_location.stock_levels.create!(variant: subject.variant)end.to raise_error(StandardError)endend@@ -540,18 +540,18 @@ describe Spree::StockLevel, type: :model dodescribe 'scopes' docontext '.with_active_stock_location' do- let(:stock_items_with_active_location) { Spree::StockLevel.with_active_stock_location }+ let(:stock_levels_with_active_location) { Spree::StockLevel.with_active_stock_location }context 'when stock location is active' dobefore { stock_location.update_column(:active, true) }- it { expect(stock_items_with_active_location).to include(subject) }+ it { expect(stock_levels_with_active_location).to include(subject) }endcontext 'when stock location is inactive' dobefore { stock_location.update_column(:active, false) }- it { expect(stock_items_with_active_location).not_to include(subject) }+ it { expect(stock_levels_with_active_location).not_to include(subject) }endend@@ -562,10 +562,10 @@ describe Spree::StockLevel, type: :model dolet(:product_in_other_store) { create(:product, store: other_store) }let!(:in_store_item) do- create(:stock_item, variant: product_in_store.default_variant, stock_location: stock_location)+ create(:stock_level, variant: product_in_store.default_variant, stock_location: stock_location)endlet!(:other_store_item) do- create(:stock_item, variant: product_in_other_store.default_variant, stock_location: stock_location)+ create(:stock_level, variant: product_in_other_store.default_variant, stock_location: stock_location)endit 'returns stock items for variants of products owned by the store' dodiff --git a/spree/core/spec/models/spree/stock_location_spec.rb b/spree/core/spec/models/spree/stock_location_spec.rbindex 059f74178e..b40b70cc52 100644--- a/spree/core/spec/models/spree/stock_location_spec.rb+++ b/spree/core/spec/models/spree/stock_location_spec.rb@@ -4,24 +4,24 @@ module Spreedescribe StockLocation, type: :model dosubject { create(:stock_location_with_items, backorderable_default: true) }- let(:stock_item) { subject.stock_items.order(:id).first }- let(:variant) { stock_item.variant }+ let(:stock_level) { subject.stock_levels.order(:id).first }+ let(:variant) { stock_level.variant }context 'handling the stock items creation after create' dolet!(:variant) { create(:variant) }before do- Spree::StockItem.delete_all+ Spree::StockLevel.delete_alldescribed_class.delete_allend- it 'creates stock_items for all variants' do+ it 'creates stock_levels for all variants' doexpect doperform_enqueued_jobs { create(:stock_location, propagate_all_variants: true) }end.to(change { Variant.count }.by(0).and(change { described_class.count }.from(0).to(1).and(- change { StockItem.count }.from(0).to(2)+ change { StockLevel.count }.from(0).to(2))))@@ -42,38 +42,38 @@ module Spreecontext 'set up' doit 'creates stock item' doexpect(subject).to receive(:propagate_variant)- subject.set_up_stock_item(variant)+ subject.set_up_stock_level(variant)endcontext 'stock item exists' do- let!(:stock_item) { subject.propagate_variant(variant) }+ let!(:stock_level) { subject.propagate_variant(variant) }it 'returns existing stock item' do- expect(subject.set_up_stock_item(variant)).to eq(stock_item)+ expect(subject.set_up_stock_level(variant)).to eq(stock_level)endendendcontext 'propagate variants' do- let(:stock_item) { subject.propagate_variant(variant) }+ let(:stock_level) { subject.propagate_variant(variant) }it 'creates a new stock item' doexpect dosubject.propagate_variant(variant)- end.to change(StockItem, :count).by(1)+ end.to change(StockLevel, :count).by(1)endcontext 'passes backorderable default config' docontext 'true' dobefore { subject.backorderable_default = true }- it { expect(stock_item.backorderable).to be true }+ it { expect(stock_level.backorderable).to be true }endcontext 'false' dobefore { subject.backorderable_default = false }- it { expect(stock_item.backorderable).to be false }+ it { expect(stock_level.backorderable).to be false }endendend@@ -85,8 +85,8 @@ module Spreebefore { subject.propagate_all_variants = true }specify do- expect(subject).to receive(:create_stock_items).and_call_original- expect(Spree::StockLocations::StockItems::CreateJob).to(+ expect(subject).to receive(:create_stock_levels).and_call_original+ expect(Spree::StockLocations::StockLevels::CreateJob).to(receive(:perform_later).once.with(subject))subject.save!@@ -97,7 +97,7 @@ module Spreebefore { subject.propagate_all_variants = false }specify do- expect(subject).not_to receive(:create_stock_items)+ expect(subject).not_to receive(:create_stock_levels)subject.save!endend@@ -105,59 +105,59 @@ module Spreeendend- it 'finds a stock_item for a variant' do- stock_item = subject.stock_item(variant)- expect(stock_item.count_on_hand).to eq 10+ it 'finds a stock_level for a variant' do+ stock_level = subject.stock_level(variant)+ expect(stock_level.count_on_hand).to eq 10end- it 'finds a stock_item for a variant by id' do- stock_item = subject.stock_item(variant.id)- expect(stock_item.variant).to eq variant+ it 'finds a stock_level for a variant by id' do+ stock_level = subject.stock_level(variant.id)+ expect(stock_level.variant).to eq variantend- it 'returns nil when stock_item is not found for variant' do+ it 'returns nil when stock_level is not found for variant' dovariant_id = variant.id + 1000- stock_item = subject.stock_item(variant_id)- expect(stock_item).to be_nil+ stock_level = subject.stock_level(variant_id)+ expect(stock_level).to be_nilend- describe '#stock_item_or_create' do+ describe '#stock_level_or_create' docontext 'without stock item' dolet!(:variant) { create(:variant) }- before { variant.stock_items.delete_all }+ before { variant.stock_levels.delete_all }context 'variant instance passed' do- it 'creates a stock_item if not found for a variant' do- stock_item = subject.stock_item_or_create(variant)- expect(stock_item.variant).to eq variant+ it 'creates a stock_level if not found for a variant' do+ stock_level = subject.stock_level_or_create(variant)+ expect(stock_level.variant).to eq variantend- it { expect { subject.stock_item_or_create(variant) }.to change(Spree::StockItem, :count) }+ it { expect { subject.stock_level_or_create(variant) }.to change(Spree::StockLevel, :count) }endcontext 'variant ID passed' do- it 'creates a stock_item if not found for a variant' do- stock_item = subject.stock_item_or_create(variant.id)- expect(stock_item.variant).to eq variant+ it 'creates a stock_level if not found for a variant' do+ stock_level = subject.stock_level_or_create(variant.id)+ expect(stock_level.variant).to eq variantend- it { expect { subject.stock_item_or_create(variant.id) }.to change(Spree::StockItem, :count) }+ it { expect { subject.stock_level_or_create(variant.id) }.to change(Spree::StockLevel, :count) }endendcontext 'with stock item' dolet!(:variant) { create(:variant) }- let!(:stock_item) { create(:stock_item, variant: variant, stock_location: subject) }+ let!(:stock_level) { create(:stock_level, variant: variant, stock_location: subject) }context 'variant instance passed' do- it { expect { subject.stock_item_or_create(variant) }.not_to change(Spree::StockItem, :count) }- it { expect(subject.stock_item_or_create(variant)).to eq(stock_item) }+ it { expect { subject.stock_level_or_create(variant) }.not_to change(Spree::StockLevel, :count) }+ it { expect(subject.stock_level_or_create(variant)).to eq(stock_level) }endcontext 'variant ID passed' do- it { expect { subject.stock_item_or_create(variant.id) }.not_to change(Spree::StockItem, :count) }- it { expect(subject.stock_item_or_create(variant.id)).to eq(stock_item) }+ it { expect { subject.stock_level_or_create(variant.id) }.not_to change(Spree::StockLevel, :count) }+ it { expect(subject.stock_level_or_create(variant.id)).to eq(stock_level) }endendend@@ -185,7 +185,7 @@ module Spreeit 'creates a stock_movement' doexpect dosubject.move variant, 5- end.to change { subject.stock_movements.where(stock_item_id: stock_item).count }.by(1)+ end.to change { subject.stock_movements.where(stock_level_id: stock_level).count }.by(1)endit 'can be deactivated' do@@ -209,9 +209,9 @@ module Spreeendcontext 'fill_status' do- let(:zero_stock_item) { subject.stock_items.order(:id).second }+ let(:zero_stock_level) { subject.stock_levels.order(:id).second }- before { allow(zero_stock_item).to receive_messages(backorderable?: true, count_on_hand: 0) }+ before { allow(zero_stock_level).to receive_messages(backorderable?: true, count_on_hand: 0) }it 'all on_hand with no backordered' doon_hand, backordered = subject.fill_status(variant, 5)@@ -226,7 +226,7 @@ module Spreeendit 'zero on_hand with all backordered' do- expect(subject).to receive(:stock_item_or_create).with(variant).and_return(zero_stock_item)+ expect(subject).to receive(:stock_level_or_create).with(variant).and_return(zero_stock_level)on_hand, backordered = subject.fill_status(variant, 20)expect(on_hand).to eq 0@@ -235,12 +235,12 @@ module Spreecontext 'when backordering is not allowed' dobefore do- allow(stock_item).to receive_messages backorderable?: false- expect(subject).to receive(:stock_item_or_create).with(variant).and_return(stock_item)+ allow(stock_level).to receive_messages backorderable?: false+ expect(subject).to receive(:stock_level_or_create).with(variant).and_return(stock_level)endit 'all on_hand' do- allow(stock_item).to receive_messages(count_on_hand: 10)+ allow(stock_level).to receive_messages(count_on_hand: 10)on_hand, backordered = subject.fill_status(variant, 5)expect(on_hand).to eq 5@@ -248,7 +248,7 @@ module Spreeendit 'some on_hand' do- allow(stock_item).to receive_messages(count_on_hand: 10)+ allow(stock_level).to receive_messages(count_on_hand: 10)on_hand, backordered = subject.fill_status(variant, 20)expect(on_hand).to eq 10@@ -256,7 +256,7 @@ module Spreeendit 'zero on_hand' do- allow(stock_item).to receive_messages(count_on_hand: 0)+ allow(stock_level).to receive_messages(count_on_hand: 0)on_hand, backordered = subject.fill_status(variant, 20)expect(on_hand).to eq 0@@ -264,14 +264,14 @@ module Spreeendend- context 'without stock_items' do+ context 'without stock_levels' dosubject { create(:stock_location) }let(:variant) { create(:base_variant) }it 'zero on_hand and one backordered' dosubject- variant.stock_items.delete_all+ variant.stock_levels.delete_allon_hand, backordered = subject.fill_status(variant, 1)expect(on_hand).to eq 0expect(backordered).to eq 1diff --git a/spree/core/spec/models/spree/stock_movement/custom_events_spec.rb b/spree/core/spec/models/spree/stock_movement/custom_events_spec.rbindex 067868c399..b981923c34 100644--- a/spree/core/spec/models/spree/stock_movement/custom_events_spec.rb+++ b/spree/core/spec/models/spree/stock_movement/custom_events_spec.rb@@ -6,8 +6,8 @@ RSpec.describe Spree::StockMovement::CustomEvents dolet(:stock_location) { create(:stock_location) }let(:product) { create(:product) }let(:variant) { product.default_variant }- let!(:stock_item) do- item = variant.stock_items.first || create(:stock_item, variant: variant, stock_location: stock_location)+ let!(:stock_level) do+ item = variant.stock_levels.first || create(:stock_level, variant: variant, stock_location: stock_location)item.update!(backorderable: false)itemend@@ -19,24 +19,24 @@ RSpec.describe Spree::StockMovement::CustomEvents dodescribe 'product.out_of_stock event' dobefore do- stock_item.set_count_on_hand(10)+ stock_level.set_count_on_hand(10)endit 'publishes product.out_of_stock when product goes out of stock' do- stock_item.stock_movements.create!(quantity: -10)+ stock_level.stock_movements.create!(quantity: -10)expect(Spree::Events).to have_received(:publish).with('product.out_of_stock', anything, anything)endit 'does not publish when product still has stock' do- stock_item.stock_movements.create!(quantity: -5)+ stock_level.stock_movements.create!(quantity: -5)expect(Spree::Events).not_to have_received(:publish).with('product.out_of_stock', anything, anything)endit 'does not publish when product was already out of stock' do# Start with product already out of stock- stock_item.set_count_on_hand(0)+ stock_level.set_count_on_hand(0)# Reset the mock to clear any events from setupRSpec::Mocks.space.proxy_for(Spree::Events).reset@@ -45,7 +45,7 @@ RSpec.describe Spree::StockMovement::CustomEvents doallow(Spree::Events).to receive(:publish)# Add some stock (this triggers back_in_stock, not out_of_stock)- stock_item.stock_movements.create!(quantity: 1)+ stock_level.stock_movements.create!(quantity: 1)expect(Spree::Events).not_to have_received(:publish).with('product.out_of_stock', anything, anything)end@@ -53,19 +53,19 @@ RSpec.describe Spree::StockMovement::CustomEvents dodescribe 'product.back_in_stock event' dobefore do- stock_item.set_count_on_hand(0)+ stock_level.set_count_on_hand(0)endit 'publishes product.back_in_stock when product comes back in stock' do- stock_item.stock_movements.create!(quantity: 10)+ stock_level.stock_movements.create!(quantity: 10)expect(Spree::Events).to have_received(:publish).with('product.back_in_stock', anything, anything)endit 'does not publish when product was already in stock' do- stock_item.set_count_on_hand(5)+ stock_level.set_count_on_hand(5)- stock_item.stock_movements.create!(quantity: 10)+ stock_level.stock_movements.create!(quantity: 10)expect(Spree::Events).not_to have_received(:publish).with('product.back_in_stock', anything, anything)end@@ -74,11 +74,11 @@ RSpec.describe Spree::StockMovement::CustomEvents dodescribe 'when events are disabled' dobefore doallow(Spree::Events).to receive(:enabled?).and_return(false)- stock_item.set_count_on_hand(10)+ stock_level.set_count_on_hand(10)endit 'does not publish any events' do- stock_item.stock_movements.create!(quantity: -10)+ stock_level.stock_movements.create!(quantity: -10)expect(Spree::Events).not_to have_received(:publish).with('product.out_of_stock', anything, anything)enddiff --git a/spree/core/spec/models/spree/stock_movement_spec.rb b/spree/core/spec/models/spree/stock_movement_spec.rbindex 019630c3ee..c7e8dc8f8f 100644--- a/spree/core/spec/models/spree/stock_movement_spec.rb+++ b/spree/core/spec/models/spree/stock_movement_spec.rb@@ -30,16 +30,16 @@ describe Spree::StockMovement, type: :model dodescribe 'validations' doit "does not allow quantity that is less than the stock item's count on hand" do- stock_item = create(:stock_item, backorderable: false)- stock_movement = build(:stock_movement, quantity: -11, stock_item: stock_item)+ stock_level = create(:stock_level, backorderable: false)+ stock_movement = build(:stock_movement, quantity: -11, stock_level: stock_level)expect(stock_movement).to be_invalidexpect(stock_movement.errors[:quantity]).to include('must be greater than or equal to -10')endit 'allows the negative quantity for a backorderable stock item' do- stock_item = create(:stock_item, adjust_count_on_hand: false, backorderable: true)- stock_movement = build(:stock_movement, quantity: -1, stock_item: stock_item)+ stock_level = create(:stock_level, adjust_count_on_hand: false, backorderable: true)+ stock_movement = build(:stock_movement, quantity: -1, stock_level: stock_level)expect(stock_movement).to be_validend@@ -62,19 +62,19 @@ describe Spree::StockMovement, type: :model dodescribe 'Instance Methods' dolet(:stock_location) { create(:stock_location_with_items) }- let(:stock_item) { stock_location.stock_items.order(:id).first }- let(:stock_movement) { build(:stock_movement, stock_item: stock_item) }+ let(:stock_level) { stock_location.stock_levels.order(:id).first }+ let(:stock_movement) { build(:stock_movement, stock_level: stock_level) }describe '.product' do- it { expect(stock_movement.product).to eq(stock_item.variant.product) }+ it { expect(stock_movement.product).to eq(stock_level.variant.product) }enddescribe '.variant' do- it { expect(stock_movement.variant).to eq(stock_item.variant) }+ it { expect(stock_movement.variant).to eq(stock_level.variant) }enddescribe '#readonly?' do- let(:stock_movement) { create(:stock_movement, stock_item: stock_item) }+ let(:stock_movement) { create(:stock_movement, stock_level: stock_level) }it 'does not update a persisted record' doexpect { stock_movement.save }.to raise_error(ActiveRecord::ReadOnlyRecord)@@ -87,24 +87,24 @@ describe Spree::StockMovement, type: :model dostub_store_preferences(track_inventory_levels: false)stock_movement.quantity = 1stock_movement.save- stock_item.reload+ stock_level.reloadendit 'does not update count on hand' do- expect(stock_item.count_on_hand).to eq(10)+ expect(stock_level.count_on_hand).to eq(10)endendcontext 'when track inventory tracking is off' dobefore do- stock_item.variant.track_inventory = false+ stock_level.variant.track_inventory = falsestock_movement.quantity = 1stock_movement.save- stock_item.reload+ stock_level.reloadendit 'does not update count on hand' do- expect(stock_item.count_on_hand).to eq(10)+ expect(stock_level.count_on_hand).to eq(10)endend@@ -112,11 +112,11 @@ describe Spree::StockMovement, type: :model dobefore dostock_movement.quantity = -1stock_movement.save- stock_item.reload+ stock_level.reloadendit 'decrements the stock item count on hand' do- expect(stock_item.count_on_hand).to eq(9)+ expect(stock_level.count_on_hand).to eq(9)endend@@ -124,11 +124,11 @@ describe Spree::StockMovement, type: :model dobefore dostock_movement.quantity = 1stock_movement.save- stock_item.reload+ stock_level.reloadendit 'increments the stock item count on hand' do- expect(stock_item.count_on_hand).to eq(11)+ expect(stock_level.count_on_hand).to eq(11)endendenddiff --git a/spree/core/spec/models/spree/stock_transfer_spec.rb b/spree/core/spec/models/spree/stock_transfer_spec.rbindex 5c58008167..c11e86e5a1 100644--- a/spree/core/spec/models/spree/stock_transfer_spec.rb+++ b/spree/core/spec/models/spree/stock_transfer_spec.rb@@ -13,8 +13,8 @@ module Spreelet(:destination_location) { create(:stock_location_with_items) }let(:source_location) { create(:stock_location_with_items) }- let(:stock_item) { source_location.stock_items.order(:id).first }- let(:variant) { stock_item.variant }+ let(:stock_level) { source_location.stock_levels.order(:id).first }+ let(:variant) { stock_level.variant }it_behaves_like 'metadata'it_behaves_like 'lifecycle events'diff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rbindex 9af3228508..3da1db53ee 100644--- a/spree/core/spec/models/spree/store_spec.rb+++ b/spree/core/spec/models/spree/store_spec.rb@@ -47,13 +47,13 @@ describe Spree::Store, type: :model, without_global_store: true doit { expect(subject.variants).to eq([product.default_variant, variant]) }- describe '#stock_items' do- let!(:stock_items) { product.stock_items }- let!(:stock_items_2) { product_2.stock_items }+ describe '#stock_levels' do+ let!(:stock_levels) { product.stock_levels }+ let!(:stock_levels_2) { product_2.stock_levels }- it { expect(stock_items).not_to be_empty }- it { expect(stock_items_2).not_to be_empty }- it { expect(subject.stock_items).to eq(stock_items) }+ it { expect(stock_levels).not_to be_empty }+ it { expect(stock_levels_2).not_to be_empty }+ it { expect(subject.stock_levels).to eq(stock_levels) }endendenddiff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rbindex 3537873348..ec39bd4ac2 100644--- a/spree/core/spec/models/spree/variant_spec.rb+++ b/spree/core/spec/models/spree/variant_spec.rb@@ -126,7 +126,7 @@ describe Spree::Variant, type: :model doend- describe '#create_default_stock_item' do+ describe '#create_default_stock_level' dolet(:new_variant) { product.variants.create(track_inventory: track_inventory) }context 'when not tracking inventory' do@@ -135,16 +135,16 @@ describe Spree::Variant, type: :model doit 'creates a default stock item' donew_variant- expect(new_variant.reload.stock_items.count).to eq(1)- expect(new_variant.stock_items[0].count_on_hand).to eq(0)- expect(new_variant.stock_items[0].backorderable).to eq(false)+ expect(new_variant.reload.stock_levels.count).to eq(1)+ expect(new_variant.stock_levels[0].count_on_hand).to eq(0)+ expect(new_variant.stock_levels[0].backorderable).to eq(false)endcontext 'when variant is created along with a stock item' dolet(:new_variant) doproduct.variants.create(track_inventory: track_inventory,- stock_items_attributes: {+ stock_levels_attributes: {'0' => {stock_location_id: create(:stock_location).id,count_on_hand: 10,@@ -157,9 +157,9 @@ describe Spree::Variant, type: :model doit 'does not create an another stock item' donew_variant- expect(new_variant.reload.stock_items.count).to eq(1)- expect(new_variant.stock_items[0].count_on_hand).to eq(10)- expect(new_variant.stock_items[0].backorderable).to eq(true)+ expect(new_variant.reload.stock_levels.count).to eq(1)+ expect(new_variant.stock_levels[0].count_on_hand).to eq(10)+ expect(new_variant.stock_levels[0].backorderable).to eq(true)endendend@@ -169,7 +169,7 @@ describe Spree::Variant, type: :model doit 'does not create a default stock item' donew_variant- expect(new_variant.reload.stock_items.count).to eq(0)+ expect(new_variant.reload.stock_levels.count).to eq(0)endend@@ -178,11 +178,11 @@ describe Spree::Variant, type: :model do# clear out previous stock itemsbefore do- variant.stock_items.delete_all+ variant.stock_levels.delete_allendit 'creates a default stock item' do- expect { variant.update!(track_inventory: false) }.to change(variant.stock_items, :count).by(1)+ expect { variant.update!(track_inventory: false) }.to change(variant.stock_levels, :count).by(1)endendend@@ -195,10 +195,10 @@ describe Spree::Variant, type: :model dosubject { variant.update!(track_inventory: false) }let!(:variant) { create(:variant, product: product, track_inventory: true) }- let!(:stock_item) { create(:stock_item, variant: variant, count_on_hand: 100) }+ let!(:stock_level) { create(:stock_level, variant: variant, count_on_hand: 100) }it 'updates stock item count on hand to 0' do- expect { subject }.to change { stock_item.reload.count_on_hand }.from(110).to(0)+ expect { subject }.to change { stock_level.reload.count_on_hand }.from(110).to(0)endend@@ -206,10 +206,10 @@ describe Spree::Variant, type: :model dosubject { variant.update!(track_inventory: true) }let!(:variant) { create(:variant, product: product, track_inventory: false) }- let!(:stock_item) { create(:stock_item, variant: variant, count_on_hand: 100) }+ let!(:stock_level) { create(:stock_level, variant: variant, count_on_hand: 100) }it 'keeps stock items' do- expect { subject }.not_to change(variant.stock_items, :count)+ expect { subject }.not_to change(variant.stock_levels, :count)endendend@@ -491,7 +491,7 @@ describe Spree::Variant, type: :model dolet!(:variant) { create(:variant) }context 'when variant has no stock items' do- before { Spree::StockItem.delete_all }+ before { Spree::StockLevel.delete_all }it { expect(subject).to eq(false) }end@@ -499,16 +499,16 @@ describe Spree::Variant, type: :model docontext 'when variant has stock items' dolet!(:variant2) { create(:variant) }- before { Spree::StockItem.update_all(backorderable: false) }+ before { Spree::StockLevel.update_all(backorderable: false) }context 'when variant stock items count_on_hand > 0' do- before { variant.stock_items.first.set_count_on_hand(1) }+ before { variant.stock_levels.first.set_count_on_hand(1) }it { expect(subject).to eq(true) }endcontext 'when variant stock items count_on_hand <= 0' do- before { variant.stock_items.first.set_count_on_hand(0) }+ before { variant.stock_levels.first.set_count_on_hand(0) }it { expect(subject).to eq(false) }@@ -522,7 +522,7 @@ describe Spree::Variant, type: :model doit { expect(variant.in_stock_or_backorderable?).to eq(false) }context 'with some variant stock item having backorderable = true' do- before { variant.stock_items.first.update(backorderable: true) }+ before { variant.stock_levels.first.update(backorderable: true) }it { expect(subject).to eq(true) }end@@ -910,28 +910,28 @@ describe Spree::Variant, type: :model dostub_store_preferences(track_inventory_levels: true)end- context 'when stock_items are not backorderable' do+ context 'when stock_levels are not backorderable' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(backorderable: false)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(backorderable: false)end- context 'when stock_items in stock' do+ context 'when stock_levels in stock' dobefore do- variant.stock_items.first.update_column(:count_on_hand, 10)+ variant.stock_levels.first.update_column(:count_on_hand, 10)end- it 'returns true if stock_items in stock' do+ it 'returns true if stock_levels in stock' doexpect(variant.in_stock?).to be trueendend- context 'when stock_items out of stock' do+ context 'when stock_levels out of stock' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(backorderable: false)- allow_any_instance_of(Spree::StockItem).to receive_messages(count_on_hand: 0)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(backorderable: false)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(count_on_hand: 0)end- it 'return false if stock_items out of stock' do+ it 'return false if stock_levels out of stock' doexpect(variant.in_stock?).to be falseendend@@ -947,14 +947,14 @@ describe Spree::Variant, type: :model doendend- context 'when stock_items are backorderable' do+ context 'when stock_levels are backorderable' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(backorderable: true)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(backorderable: true)end- context 'when stock_items out of stock' do+ context 'when stock_levels out of stock' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(count_on_hand: 0)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(count_on_hand: 0)endit 'in_stock? returns false' do@@ -980,53 +980,53 @@ describe Spree::Variant, type: :model doenddescribe '#purchasable?' do- context 'when stock_items are not backorderable' do+ context 'when stock_levels are not backorderable' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(backorderable: false)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(backorderable: false)end- context 'when stock_items in stock' do+ context 'when stock_levels in stock' dobefore do- variant.stock_items.first.update_column(:count_on_hand, 10)+ variant.stock_levels.first.update_column(:count_on_hand, 10)end- it 'returns true if stock_items in stock' do+ it 'returns true if stock_levels in stock' doexpect(variant.purchasable?).to be trueendend- context 'when stock_items out of stock' do+ context 'when stock_levels out of stock' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(count_on_hand: 0)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(count_on_hand: 0)end- it 'return false if stock_items out of stock' do+ it 'return false if stock_levels out of stock' doexpect(variant.purchasable?).to be falseendendend- context 'when stock_items are out of stock' do+ context 'when stock_levels are out of stock' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(count_on_hand: 0)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(count_on_hand: 0)endcontext 'when stock item are backorderable' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(backorderable: true)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(backorderable: true)end- it 'returns true if stock_items are backorderable' do+ it 'returns true if stock_levels are backorderable' doexpect(variant.purchasable?).to be trueendend- context 'when stock_items are not backorderable' do+ context 'when stock_levels are not backorderable' dobefore do- allow_any_instance_of(Spree::StockItem).to receive_messages(backorderable: false)+ allow_any_instance_of(Spree::StockLevel).to receive_messages(backorderable: false)end- it 'return false if stock_items are not backorderable' do+ it 'return false if stock_levels are not backorderable' doexpect(variant.purchasable?).to be falseendend@@ -1164,7 +1164,7 @@ describe Spree::Variant, type: :model doenddescribe 'stock movements' do- let!(:movement) { create(:stock_movement, stock_item: variant.stock_items.first) }+ let!(:movement) { create(:stock_movement, stock_level: variant.stock_levels.first) }it 'builds out collection just fine through stock items' doexpect(variant.stock_movements.to_a).not_to be_empty@@ -1176,7 +1176,7 @@ describe Spree::Variant, type: :model doin_stock_variant = create(:variant)create(:variant) # out_of_stock_variant- in_stock_variant.stock_items.first.update_column(:count_on_hand, 10)+ in_stock_variant.stock_levels.first.update_column(:count_on_hand, 10)expect(Spree::Variant.in_stock).to eq [in_stock_variant]end@@ -1302,12 +1302,12 @@ describe Spree::Variant, type: :model doendit 'returns false when out of stock and not backorderable' do- variant.stock_items.first.update(backorderable: false)+ variant.stock_levels.first.update(backorderable: false)expect(variant.backordered?).to eq(false)endit 'returns false when there is available item in stock' do- variant.stock_items.first.update(count_on_hand: 10)+ variant.stock_levels.first.update(count_on_hand: 10)expect(variant.backordered?).to eq(false)endend@@ -1502,27 +1502,27 @@ describe Spree::Variant, type: :model doendend- describe '#stock_items= (hash array)' do+ describe '#stock_levels= (hash array)' dolet!(:stock_location) { Spree::StockLocation.first || create(:stock_location) }let!(:stock_location_2) { create(:stock_location, name: 'Warehouse 2') }context 'on persisted variant' doit 'upserts stock by location' do- variant.stock_items = [+ variant.stock_levels = [{ stock_location_id: stock_location.id, count_on_hand: 50, backorderable: true }]- si = variant.stock_items.find_by(stock_location: stock_location)+ si = variant.stock_levels.find_by(stock_location: stock_location)expect(si.count_on_hand).to eq(50)expect(si.backorderable).to eq(true)endit 'accepts prefixed IDs for stock_location_id' do- variant.stock_items = [+ variant.stock_levels = [{ stock_location_id: stock_location.prefixed_id, count_on_hand: 30 }]- si = variant.stock_items.find_by(stock_location: stock_location)+ si = variant.stock_levels.find_by(stock_location: stock_location)expect(si.count_on_hand).to eq(30)end@@ -1530,25 +1530,25 @@ describe Spree::Variant, type: :model dovariant.set_stock(10, false, stock_location)variant.set_stock(5, false, stock_location_2)- variant.stock_items = [+ variant.stock_levels = [{ stock_location_id: stock_location.id, count_on_hand: 20 }]- expect(variant.stock_items.find_by(stock_location: stock_location).count_on_hand).to eq(20)- expect(Spree::StockItem.unscoped.find_by(variant: variant, stock_location: stock_location_2).deleted_at).not_to be_nil+ expect(variant.stock_levels.find_by(stock_location: stock_location).count_on_hand).to eq(20)+ expect(Spree::StockLevel.unscoped.find_by(variant: variant, stock_location: stock_location_2).deleted_at).not_to be_nilendendcontext 'on new variant' dolet(:new_variant) { build(:variant, product: variant.product) }- it 'defers stock_items creation to after_create' do- new_variant.stock_items = [+ it 'defers stock_levels creation to after_create' do+ new_variant.stock_levels = [{ stock_location_id: stock_location.prefixed_id, count_on_hand: 99 }]new_variant.save!- si = new_variant.stock_items.find_by(stock_location: stock_location)+ si = new_variant.stock_levels.find_by(stock_location: stock_location)expect(si.count_on_hand).to eq(99)endenddiff --git a/spree/core/spec/services/spree/carts/create_spec.rb b/spree/core/spec/services/spree/carts/create_spec.rbindex a9362ce299..2a33f8b7d1 100644--- a/spree/core/spec/services/spree/carts/create_spec.rb+++ b/spree/core/spec/services/spree/carts/create_spec.rb@@ -109,8 +109,8 @@ module Spreelet(:variant2) { create(:variant) }before do- variant.stock_items.first.update!(count_on_hand: 10)- variant2.stock_items.first.update!(count_on_hand: 10)+ variant.stock_levels.first.update!(count_on_hand: 10)+ variant2.stock_levels.first.update!(count_on_hand: 10)store.products << variant.product unless store.products.include?(variant.product)store.products << variant2.product unless store.products.include?(variant2.product)end@@ -137,7 +137,7 @@ module Spreelet(:variant) { create(:variant) }before do- variant.stock_items.first.update!(count_on_hand: 10)+ variant.stock_levels.first.update!(count_on_hand: 10)store.products << variant.product unless store.products.include?(variant.product)enddiff --git a/spree/core/spec/services/spree/carts/destroy_spec.rb b/spree/core/spec/services/spree/carts/destroy_spec.rbindex e49067a94a..8399ec9a6d 100644--- a/spree/core/spec/services/spree/carts/destroy_spec.rb+++ b/spree/core/spec/services/spree/carts/destroy_spec.rb@@ -68,11 +68,11 @@ module Spreecontext 'with stock reservations' dolet(:line_item) { cart.line_items.first }let!(:reservation) do- line_item.variant.stock_items.first.update!(backorderable: false)- line_item.variant.stock_items.first.set_count_on_hand(10)+ line_item.variant.stock_levels.first.update!(backorderable: false)+ line_item.variant.stock_levels.first.set_count_on_hand(10)create(:stock_reservation,- stock_item: line_item.variant.stock_items.first,+ stock_level: line_item.variant.stock_levels.first,line_item: line_item,cart: cart,quantity: line_item.quantity,diff --git a/spree/core/spec/services/spree/carts/empty_spec.rb b/spree/core/spec/services/spree/carts/empty_spec.rbindex ceb6b41f46..668323c2d2 100644--- a/spree/core/spec/services/spree/carts/empty_spec.rb+++ b/spree/core/spec/services/spree/carts/empty_spec.rb@@ -61,11 +61,11 @@ module Spreelet(:line_item) { cart.line_items.first }before do- line_item.variant.stock_items.first.update!(backorderable: false)- line_item.variant.stock_items.first.set_count_on_hand(10)+ line_item.variant.stock_levels.first.update!(backorderable: false)+ line_item.variant.stock_levels.first.set_count_on_hand(10)create(:stock_reservation,- stock_item: line_item.variant.stock_items.first,+ stock_level: line_item.variant.stock_levels.first,line_item: line_item,cart: cart,quantity: line_item.quantity,diff --git a/spree/core/spec/services/spree/carts/remove_line_item_spec.rb b/spree/core/spec/services/spree/carts/remove_line_item_spec.rbindex 56e7d088f7..55a96279b4 100644--- a/spree/core/spec/services/spree/carts/remove_line_item_spec.rb+++ b/spree/core/spec/services/spree/carts/remove_line_item_spec.rb@@ -52,8 +52,8 @@ module Spreebefore docart.line_items.each do |cart_line_item|- cart_line_item.variant.stock_items.first.update!(backorderable: false)- cart_line_item.variant.stock_items.first.set_count_on_hand(10)+ cart_line_item.variant.stock_levels.first.update!(backorderable: false)+ cart_line_item.variant.stock_levels.first.set_count_on_hand(10)endend@@ -63,7 +63,7 @@ module Spreeit 'reservation for the removed line item is destroyed via dependent: :destroy' docreate(:stock_reservation,- stock_item: line_item.variant.stock_items.first,+ stock_level: line_item.variant.stock_levels.first,line_item: line_item,cart: cart,quantity: line_item.quantity,@@ -78,7 +78,7 @@ module Spreeit 'remaining line items get a fresh reservation pass' docreate(:stock_reservation,- stock_item: other_line_item.variant.stock_items.first,+ stock_level: other_line_item.variant.stock_levels.first,line_item: other_line_item,cart: cart,quantity: other_line_item.quantity,@@ -106,10 +106,10 @@ module Spreeit 'returns failure and rolls back the destroy when re-reservation fails' do# Bump the remaining item's quantity above its stock so re-reservation- # after the destroy fails. count_on_hand stays > 0 so select_stock_item+ # after the destroy fails. count_on_hand stays > 0 so select_stock_level# still picks the row.other_line_item.update_column(:quantity, 5)- other_line_item.variant.stock_items.first.set_count_on_hand(2)+ other_line_item.variant.stock_levels.first.set_count_on_hand(2)line_item_count_before = cart.line_items.countresult = subject.call(cart: cart, line_item: line_item)diff --git a/spree/core/spec/services/spree/carts/remove_out_of_stock_items_spec.rb b/spree/core/spec/services/spree/carts/remove_out_of_stock_items_spec.rbindex 0b96314e82..7d2a41d7ef 100644--- a/spree/core/spec/services/spree/carts/remove_out_of_stock_items_spec.rb+++ b/spree/core/spec/services/spree/carts/remove_out_of_stock_items_spec.rb@@ -37,7 +37,7 @@ RSpec.describe Spree::Carts::RemoveOutOfStockItems doendcontext 'when product is out of stock' do- before { product.stock_items.update_all(count_on_hand: 0, backorderable: false) }+ before { product.stock_levels.update_all(count_on_hand: 0, backorderable: false) }it 'removes line item and returns out of stock message' do_cart, messages, _warnings = execute.value@@ -83,12 +83,12 @@ RSpec.describe Spree::Carts::RemoveOutOfStockItems dobefore dostub_store_preferences(stock_reservations_enabled: true)- variant.stock_items.update_all(count_on_hand: 1, backorderable: false)+ variant.stock_levels.update_all(count_on_hand: 1, backorderable: false)line_item.update!(quantity: 1)- variant.stock_items.each do |stock_item|+ variant.stock_levels.each do |stock_level|create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: line_item,cart: cart,quantity: 1,diff --git a/spree/core/spec/services/spree/carts/set_quantity_spec.rb b/spree/core/spec/services/spree/carts/set_quantity_spec.rbindex 46f97ea5a4..a583d6b9db 100644--- a/spree/core/spec/services/spree/carts/set_quantity_spec.rb+++ b/spree/core/spec/services/spree/carts/set_quantity_spec.rb@@ -10,8 +10,8 @@ module Spreecontext 'with non-backorderable item' dobefore do- line_item.variant.stock_items.first.update(backorderable: false)- line_item.variant.stock_items.first.update(count_on_hand: 5)+ line_item.variant.stock_levels.first.update(backorderable: false)+ line_item.variant.stock_levels.first.update(count_on_hand: 5)endcontext 'with sufficient stock quantity' do@@ -47,8 +47,8 @@ module Spreecontext 'stock reservations' dobefore do- line_item.variant.stock_items.first.update!(backorderable: false)- line_item.variant.stock_items.first.set_count_on_hand(20)+ line_item.variant.stock_levels.first.update!(backorderable: false)+ line_item.variant.stock_levels.first.set_count_on_hand(20)endcontext 'when the cart is mid-checkout' do@@ -73,7 +73,7 @@ module Spreeit 'fails when the new quantity exceeds available stock and rolls back' dosubject.call(cart: cart, line_item: line_item, quantity: 2)- line_item.variant.stock_items.first.set_count_on_hand(3)+ line_item.variant.stock_levels.first.set_count_on_hand(3)result = subject.call(cart: cart, line_item: line_item, quantity: 5)diff --git a/spree/core/spec/services/spree/carts/update_spec.rb b/spree/core/spec/services/spree/carts/update_spec.rbindex effab3aad4..2e7e22996b 100644--- a/spree/core/spec/services/spree/carts/update_spec.rb+++ b/spree/core/spec/services/spree/carts/update_spec.rb@@ -645,7 +645,7 @@ module Spreelet(:variant) { create(:variant) }before do- variant.stock_items.first.update!(count_on_hand: 10)+ variant.stock_levels.first.update!(count_on_hand: 10)store.products << variant.product unless store.products.include?(variant.product)end@@ -758,8 +758,8 @@ module Spreeendbefore do- cart.line_items.first.variant.stock_items.first.update!(backorderable: false)- cart.line_items.first.variant.stock_items.first.set_count_on_hand(20)+ cart.line_items.first.variant.stock_levels.first.update!(backorderable: false)+ cart.line_items.first.variant.stock_levels.first.set_count_on_hand(20)endcontext 'cart → checkout transition' do@@ -799,11 +799,11 @@ module Spreelet(:params) { { email: '[email protected]', shipping_address: address_params } }before do- # Bump line_item quantity above what stock_item can satisfy so that- # select_stock_item still picks the item (count_on_hand > 0) but+ # Bump line_item quantity above what stock_level can satisfy so that+ # select_stock_level still picks the item (count_on_hand > 0) but# the availability check fails.cart.line_items.first.update_column(:quantity, 5)- cart.line_items.first.variant.stock_items.first.set_count_on_hand(2)+ cart.line_items.first.variant.stock_levels.first.set_count_on_hand(2)endit 'returns failure and rolls back the cart update' dodiff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rbindex 6e044eeda2..73ee7374f5 100644--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb@@ -51,9 +51,9 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(default_variant.sku).to be_blankexpect(default_variant.price_in('USD').amount.to_f).to eq 62.99expect(default_variant.weight.to_f).to eq 0.0- expect(default_variant.stock_items.first.count_on_hand).to eq 100- expect(default_variant.stock_items.first.backorderable).to eq true- expect(default_variant.stock_items.first.stock_location).to eq store.default_stock_location+ expect(default_variant.stock_levels.first.count_on_hand).to eq 100+ expect(default_variant.stock_levels.first.backorderable).to eq true+ expect(default_variant.stock_levels.first.stock_location).to eq store.default_stock_locationendit 'enqueues AssignTagsJob' do@@ -92,22 +92,22 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service dolet!(:existing_product) { create(:product, slug: 'denim-shirt', name: 'Old Name') }before do- stock_item = existing_product.default_variant.stock_items.find_or_initialize_by(stock_location: store.default_stock_location)- stock_item.count_on_hand = 50- stock_item.backorderable = false- stock_item.save!+ stock_level = existing_product.default_variant.stock_levels.find_or_initialize_by(stock_location: store.default_stock_location)+ stock_level.count_on_hand = 50+ stock_level.backorderable = false+ stock_level.save!endit 'updates inventory_count and inventory_backorderable' do- stock_item = existing_product.default_variant.stock_items.find_by(stock_location: store.default_stock_location)- expect(stock_item.count_on_hand).to eq 50- expect(stock_item.backorderable).to eq false+ stock_level = existing_product.default_variant.stock_levels.find_by(stock_location: store.default_stock_location)+ expect(stock_level.count_on_hand).to eq 50+ expect(stock_level.backorderable).to eq falsesubject.process!- stock_item.reload- expect(stock_item.count_on_hand).to eq 100- expect(stock_item.backorderable).to eq true+ stock_level.reload+ expect(stock_level.count_on_hand).to eq 100+ expect(stock_level.backorderable).to eq trueexpect(existing_product.reload.name).to eq 'Denim Shirt'end@@ -242,22 +242,22 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service docontext 'when updating inventory values' dobefore do- stock_item = variant.stock_items.find_or_initialize_by(stock_location: store.default_stock_location)- stock_item.count_on_hand = 50- stock_item.backorderable = false- stock_item.save!+ stock_level = variant.stock_levels.find_or_initialize_by(stock_location: store.default_stock_location)+ stock_level.count_on_hand = 50+ stock_level.backorderable = false+ stock_level.save!endit 'updates inventory_count and inventory_backorderable' do- stock_item = variant.stock_items.find_by(stock_location: store.default_stock_location)- expect(stock_item.count_on_hand).to eq 50- expect(stock_item.backorderable).to eq false+ stock_level = variant.stock_levels.find_by(stock_location: store.default_stock_location)+ expect(stock_level.count_on_hand).to eq 50+ expect(stock_level.backorderable).to eq falsesubject.process!- stock_item.reload- expect(stock_item.count_on_hand).to eq 100- expect(stock_item.backorderable).to eq true+ stock_level.reload+ expect(stock_level.count_on_hand).to eq 100+ expect(stock_level.backorderable).to eq trueendendenddiff --git a/spree/core/spec/services/spree/orders/create_spec.rb b/spree/core/spec/services/spree/orders/create_spec.rbindex 30e4fd119d..0e92aff118 100644--- a/spree/core/spec/services/spree/orders/create_spec.rb+++ b/spree/core/spec/services/spree/orders/create_spec.rb@@ -205,7 +205,7 @@ module Spree# fires and UpsertItems returns a failing line_item.let(:out_of_stock_product) docreate(:product).tap do |p|- p.default_variant.stock_items.update_all(count_on_hand: 0, backorderable: false)+ p.default_variant.stock_levels.update_all(count_on_hand: 0, backorderable: false)endendlet(:params) dodiff --git a/spree/core/spec/services/spree/products/duplicator_spec.rb b/spree/core/spec/services/spree/products/duplicator_spec.rbindex 2d786c7b0f..5a97735c4e 100644--- a/spree/core/spec/services/spree/products/duplicator_spec.rb+++ b/spree/core/spec/services/spree/products/duplicator_spec.rb@@ -24,7 +24,7 @@ RSpec.describe Spree::Products::Duplicator donew_image.save!product.default_variant.update!(barcode: '1234567890')- product.default_variant.stock_items.last.update!(count_on_hand: 100, backorderable: true)+ product.default_variant.stock_levels.last.update!(count_on_hand: 100, backorderable: true)endit { is_expected.to be_success }@@ -120,8 +120,8 @@ RSpec.describe Spree::Products::Duplicator dolet(:new_product) { duplicate.value }before do- variant1.stock_items.last.update!(count_on_hand: 100, backorderable: true)- variant2.stock_items.last.update!(count_on_hand: 200, backorderable: false)+ variant1.stock_levels.last.update!(count_on_hand: 100, backorderable: true)+ variant2.stock_levels.last.update!(count_on_hand: 200, backorderable: false)endit 'duplicates the variants' dodiff --git a/spree/core/spec/services/spree/stock_locations/stock_items/create_spec.rb b/spree/core/spec/services/spree/stock_locations/stock_items/create_spec.rbindex dfd19154c9..f1ced6a5e7 100644--- a/spree/core/spec/services/spree/stock_locations/stock_items/create_spec.rb+++ b/spree/core/spec/services/spree/stock_locations/stock_items/create_spec.rb@@ -5,75 +5,21 @@ module Spreesubject { described_class }let!(:stock_location) { create(:stock_location_with_items) }- let!(:unrelated_variant) { create(:variant) }- let(:result) { subject.call(stock_location: stock_location) }- let(:stock_location_class) { stock_location.class }- describe '#call' do- let(:time_current) { Time.local(1990) }+ it 'warns that it has been renamed' do+ expect(Spree::Deprecation).to receive(:warn).with(/StockLevels::Create/)- context 'with prepared stock items' do- context 'with stock items in the db' do- before { Spree::StockItem.find(Spree::StockItem.ids.sample).delete }-- it 'inserts stock items without duplicates' do- expect { result }.to change { stock_location.stock_items.count }.by(1)- expect(stock_location.stock_items.count).to eq(Spree::Variant.count)- end- end-- context 'without stock items in the db' do- before do- # Delete all stock_location.stock_items to start counting from 0.- stock_location.stock_items.unscope(:where).delete_all- end-- let(:created_stock_item) { stock_location.stock_items.find_by(variant_id: unrelated_variant.id) }- let(:created_stock_item_attrs) do- created_stock_item.attributes.values_at(- 'stock_location_id', 'variant_id', 'backorderable', 'created_at', 'updated_at'- )- end-- it 'inserts the stock location stock items' do- expect { result }.to change { stock_location.stock_items.count }.from(0).to(4)- end-- it 'sets the stock location data necessary for the inserted stock items' do- Timecop.freeze(time_current) do- result- expect(created_stock_item_attrs).to(- eq([- stock_location.id,- unrelated_variant.id,- stock_location.backorderable_default,- time_current,- time_current- ])- )- end- end-- it 'invalidates the Variant cache' do- expect(Spree::Variant).to receive(:touch_all).once- result- end- end- end-- context 'without prepared stock items' do- before { Spree::Variant.delete_all }+ subject.call(stock_location: stock_location)+ end- it 'does not insert stock items' do- expect(stock_location.stock_items).not_to receive(:insert_all)- result- end+ # The work itself only happens in the renamed service, so seeing the levels+ # appear is what proves the shim delegates.+ it 'still creates the missing stock levels' do+ allow(Spree::Deprecation).to receive(:warn)+ stock_location.stock_levels.unscope(:where).delete_all- it 'does not invalidates the Variant cache' do- expect(Spree::Variant).not_to receive(:touch_all)- result- end- end+ expect { subject.call(stock_location: stock_location) }.+ to change { stock_location.stock_levels.count }.from(0).to(Spree::Variant.count)endendenddiff --git a/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rb b/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rbnew file mode 100644index 0000000000..7839c0c386--- /dev/null+++ b/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rb@@ -0,0 +1,79 @@+require 'spec_helper'++module Spree+ describe StockLocations::StockLevels::Create do+ subject { described_class }++ let!(:stock_location) { create(:stock_location_with_items) }+ let!(:unrelated_variant) { create(:variant) }+ let(:result) { subject.call(stock_location: stock_location) }+ let(:stock_location_class) { stock_location.class }++ describe '#call' do+ let(:time_current) { Time.local(1990) }++ context 'with prepared stock levels' do+ context 'with stock levels in the db' do+ before { Spree::StockLevel.find(Spree::StockLevel.ids.sample).delete }++ it 'inserts stock levels without duplicates' do+ expect { result }.to change { stock_location.stock_levels.count }.by(1)+ expect(stock_location.stock_levels.count).to eq(Spree::Variant.count)+ end+ end++ context 'without stock levels in the db' do+ before do+ # Delete all stock_location.stock_levels to start counting from 0.+ stock_location.stock_levels.unscope(:where).delete_all+ end++ let(:created_stock_level) { stock_location.stock_levels.find_by(variant_id: unrelated_variant.id) }+ let(:created_stock_level_attrs) do+ created_stock_level.attributes.values_at(+ 'stock_location_id', 'variant_id', 'backorderable', 'created_at', 'updated_at'+ )+ end++ it 'inserts the stock location stock levels' do+ expect { result }.to change { stock_location.stock_levels.count }.from(0).to(4)+ end++ it 'sets the stock location data necessary for the inserted stock levels' do+ Timecop.freeze(time_current) do+ result+ expect(created_stock_level_attrs).to(+ eq([+ stock_location.id,+ unrelated_variant.id,+ stock_location.backorderable_default,+ time_current,+ time_current+ ])+ )+ end+ end++ it 'invalidates the Variant cache' do+ expect(Spree::Variant).to receive(:touch_all).once+ result+ end+ end+ end++ context 'without prepared stock levels' do+ before { Spree::Variant.delete_all }++ it 'does not insert stock levels' do+ expect(stock_location.stock_levels).not_to receive(:insert_all)+ result+ end++ it 'does not invalidates the Variant cache' do+ expect(Spree::Variant).not_to receive(:touch_all)+ result+ end+ end+ end+ end+enddiff --git a/spree/core/spec/services/spree/stock_reservations/reserve_spec.rb b/spree/core/spec/services/spree/stock_reservations/reserve_spec.rbindex a583769692..fb87ba50c4 100644--- a/spree/core/spec/services/spree/stock_reservations/reserve_spec.rb+++ b/spree/core/spec/services/spree/stock_reservations/reserve_spec.rb@@ -3,8 +3,8 @@ require 'spec_helper'describe Spree::StockReservations::Reserve dolet(:store) { create(:store) }let(:variant) { create(:variant) }- let!(:stock_location) { variant.stock_items.first.stock_location.tap { |sl| sl.update!(active: true) } }- let!(:stock_item) { variant.stock_items.first.tap { |si| si.update!(backorderable: false); si.set_count_on_hand(10) } }+ let!(:stock_location) { variant.stock_levels.first.stock_location.tap { |sl| sl.update!(active: true) } }+ let!(:stock_level) { variant.stock_levels.first.tap { |si| si.update!(backorderable: false); si.set_count_on_hand(10) } }let(:order) { create(:order, store: store) }let!(:line_item) { create(:line_item, order: order, variant: variant, quantity: 3) }@@ -18,7 +18,7 @@ describe Spree::StockReservations::Reserve doexpect(result).to be_successreservation = Spree::StockReservation.last- expect(reservation.stock_item).to eq(stock_item)+ expect(reservation.stock_level).to eq(stock_level)expect(reservation.line_item).to eq(line_item)expect(reservation.order).to eq(order)expect(reservation.quantity).to eq(3)@@ -38,19 +38,19 @@ describe Spree::StockReservations::Reserve doendit 'fails when stock is insufficient and rolls back' do- stock_item.set_count_on_hand(1)+ stock_level.set_count_on_hand(1)expect { result }.not_to change(Spree::StockReservation, :count)expect(result).to be_failureexpect(result.error.to_s).to include('available')endit 'subtracts other orders\' reservations when checking availability' do- stock_item.set_count_on_hand(5)+ stock_level.set_count_on_hand(5)other_order = create(:order, store: store)other_line_item = create(:line_item, order: other_order, variant: variant, quantity: 4)create(:stock_reservation,- stock_item: stock_item,+ stock_level: stock_level,line_item: other_line_item,order: other_order,quantity: 4,@@ -61,9 +61,9 @@ describe Spree::StockReservations::Reserve doexpect(result).to be_failureend- it 'accumulates per stock_item across line items so the same SKU twice cannot oversell' do+ it 'accumulates per stock_level across line items so the same SKU twice cannot oversell' do# 5 units on hand, two line items each wanting 3 → total demand 6, must fail.- stock_item.set_count_on_hand(5)+ stock_level.set_count_on_hand(5)create(:line_item, order: order, variant: variant, quantity: 3)expect(result).to be_failure@@ -71,7 +71,7 @@ describe Spree::StockReservations::Reserve doendit 'skips backorderable stock items' do- stock_item.update!(backorderable: true)+ stock_level.update!(backorderable: true)expect { result }.not_to change(Spree::StockReservation, :count)expect(result).to be_successenddiff --git a/spree/core/spec/workflows/spree/carts/add_item_spec.rb b/spree/core/spec/workflows/spree/carts/add_item_spec.rbindex 99e8783e0f..15be638d1f 100644--- a/spree/core/spec/workflows/spree/carts/add_item_spec.rb+++ b/spree/core/spec/workflows/spree/carts/add_item_spec.rb@@ -173,7 +173,7 @@ module Spreecontext 'variant does not have the desired quantity' dolet(:execute) { subject.call(cart: cart, variant: variant, quantity: 10) }- before { variant.stock_items.first.update backorderable: false }+ before { variant.stock_levels.first.update backorderable: false }it 'fails without adding a line item' doexpect(execute).to be_failure@@ -201,8 +201,8 @@ module Spreebefore do# No stock; the backorder_limit is the cap (not backorderable).- variant.stock_items.first.update!(backorderable: false)- variant.stock_items.first.set_count_on_hand(0)+ variant.stock_levels.first.update!(backorderable: false)+ variant.stock_levels.first.set_count_on_hand(0)variant.update!(preorderable: true, backorder_limit: 5)# Scheduled to publish later — embargoed unless preorderable.@@ -236,7 +236,7 @@ module Spreebefore do# Plenty of stock so the publish embargo is the only rejection cause.variant.update!(preorderable: false)- variant.stock_items.first.set_count_on_hand(10)+ variant.stock_levels.first.set_count_on_hand(10)endit 'is rejected because the product is not yet published' do@@ -277,8 +277,8 @@ module Spreelet(:variant) { create(:variant, price: 20) }before do- variant.stock_items.first.update!(backorderable: false)- variant.stock_items.first.set_count_on_hand(10)+ variant.stock_levels.first.update!(backorderable: false)+ variant.stock_levels.first.set_count_on_hand(10)endcontext 'when the cart is mid-checkout' do@@ -290,7 +290,7 @@ module Spreeendit 'fails when adding more than available and rolls back the line item' do- variant.stock_items.first.set_count_on_hand(0)+ variant.stock_levels.first.set_count_on_hand(0)result = subject.call(cart: cart, variant: variant, quantity: 1)@@ -324,7 +324,7 @@ module Spreebefore dovariant.update!(preorderable: true, backorder_limit: 5)- variant.stock_items.first.set_count_on_hand(2)+ variant.stock_levels.first.set_count_on_hand(2)endit 'skips reservation and accepts quantities beyond on-hand stock' dodiff --git a/spree/core/spec/workflows/spree/carts/complete_spec.rb b/spree/core/spec/workflows/spree/carts/complete_spec.rbindex 5e58f02b05..d7ec68d6d4 100644--- a/spree/core/spec/workflows/spree/carts/complete_spec.rb+++ b/spree/core/spec/workflows/spree/carts/complete_spec.rb@@ -281,8 +281,8 @@ module Spreedescribe 'stock reservations' doit 'releases reservations on successful completion' doline_item = ready_cart.line_items.first- stock_item = line_item.variant.stock_items.first- Spree::StockReservation.create!(cart: ready_cart, order: nil, line_item: line_item, stock_item: stock_item, quantity: 1, expires_at: 1.hour.from_now)+ stock_level = line_item.variant.stock_levels.first+ Spree::StockReservation.create!(cart: ready_cart, order: nil, line_item: line_item, stock_level: stock_level, quantity: 1, expires_at: 1.hour.from_now)expect { described_class.call(cart: ready_cart) }.to change { Spree::StockReservation.count }.by(-1)enddiff --git a/spree/core/spec/workflows/spree/carts/upsert_items_spec.rb b/spree/core/spec/workflows/spree/carts/upsert_items_spec.rbindex 35548ab528..51b9dba113 100644--- a/spree/core/spec/workflows/spree/carts/upsert_items_spec.rb+++ b/spree/core/spec/workflows/spree/carts/upsert_items_spec.rb@@ -10,7 +10,7 @@ module Spreebefore do[variant, variant2].each do |v|- v.stock_items.first.update!(count_on_hand: 10)+ v.stock_levels.first.update!(count_on_hand: 10)store.products << v.product unless store.products.include?(v.product)endend@@ -167,7 +167,7 @@ module Spreelet(:other_variant) { create(:variant) }before do- other_variant.stock_items.first.update!(count_on_hand: 10)+ other_variant.stock_levels.first.update!(count_on_hand: 10)other_store.products << other_variant.productend@@ -326,7 +326,7 @@ module Spree]end- before { variant2.stock_items.first.update!(count_on_hand: 1, backorderable: false) }+ before { variant2.stock_levels.first.update!(count_on_hand: 1, backorderable: false) }it 'warns and keeps the rest of the batch' doworkflow = described_class.newdiff --git a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rbindex 6c799a3ab8..f90485b66e 100644--- a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb@@ -78,7 +78,7 @@ RSpec.describe 'Spree::Claims workflows' doit 'ignores the refund method when the resolution moves no money' doreplacing = create(:approved_claim, store: store, order: order, send_replacement: true)- replacing.claim_line_items.each { |line| line.variant.stock_items.first&.set_count_on_hand(10) }+ replacing.claim_line_items.each { |line| line.variant.stock_levels.first&.set_count_on_hand(10) }result = Spree::Claims::Resolve.call(claim: replacing, resolution: 'replacement', refund_method: 'crypto')@@ -103,7 +103,7 @@ RSpec.describe 'Spree::Claims workflows' doit 'ships a replacement' doclaim = create(:approved_claim, store: store, order: order, send_replacement: true)- claim.claim_line_items.each { |line| line.variant.stock_items.first&.set_count_on_hand(10) }+ claim.claim_line_items.each { |line| line.variant.stock_levels.first&.set_count_on_hand(10) }result = Spree::Claims::Resolve.call(claim: claim, resolution: 'replacement')diff --git a/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rb b/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rbindex b06660f97b..75265fbac3 100644--- a/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/exchanges/lifecycle_spec.rb@@ -64,24 +64,24 @@ RSpec.describe 'Spree::Exchanges workflows' doit 'receives and restocks the returned variant' doline = exchange.exchange_line_items.first- stock_item = exchange.stock_location.stock_item_or_create(line.original_variant)+ stock_level = exchange.stock_location.stock_level_or_create(line.original_variant)expect { Spree::Exchanges::Receive.call(exchange: exchange) }.- to change { stock_item.reload.count_on_hand }.by(line.quantity)+ to change { stock_level.reload.count_on_hand }.by(line.quantity)expect(exchange.reload).to be_receivedendit 'does not restock what came back damaged' doline = exchange.exchange_line_items.first- stock_item = exchange.stock_location.stock_item_or_create(line.original_variant)+ stock_level = exchange.stock_location.stock_level_or_create(line.original_variant)Spree::Exchanges::Receive.call(exchange: exchange,items: [{ exchange_line_item: line, quantity: 1, resellable: false }])- expect(stock_item.reload.count_on_hand).to eq(stock_item.count_on_hand)+ expect(stock_level.reload.count_on_hand).to eq(stock_level.count_on_hand)expect(line.reload.resellable).to be(false)endend@@ -97,7 +97,7 @@ RSpec.describe 'Spree::Exchanges workflows' doit 'creates a replacement fulfillment and marks it fulfilled' doexchange.exchange_line_items.each do |line|- line.new_variant.stock_items.first&.set_count_on_hand(10)+ line.new_variant.stock_levels.first&.set_count_on_hand(10)endresult = Spree::Exchanges::Fulfill.call(exchange: exchange)@@ -112,7 +112,7 @@ RSpec.describe 'Spree::Exchanges workflows' docontext 'when the replacement is cheaper, so a credit is due' dolet(:cheap_exchange) doexchange.exchange_line_items.each do |line|- line.new_variant.stock_items.first&.set_count_on_hand(10)+ line.new_variant.stock_levels.first&.set_count_on_hand(10)line.new_variant.prices.first&.update!(amount: 1)endexchangediff --git a/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb b/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rbindex fd6ff6f188..830d429e83 100644--- a/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb+++ b/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb@@ -17,9 +17,9 @@ module Spreeit 'puts the units back on the shelf' dovariant = fulfillment.fulfillment_items.first.variant- stock_item = fulfillment.stock_location.stock_item(variant)+ stock_level = fulfillment.stock_location.stock_level(variant)- expect { execute }.to change { stock_item.reload.count_on_hand }.by(+ expect { execute }.to change { stock_level.reload.count_on_hand }.by(fulfillment.fulfillment_items.where(variant_id: variant.id).sum(:quantity))enddiff --git a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rbindex 58d3f40c96..38a97f8655 100644--- a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb+++ b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb@@ -155,10 +155,10 @@ module Spreeit 'takes the units back off the shelf' dovariant = fulfillment.fulfillment_items.first.variant- stock_item = fulfillment.stock_location.stock_item(variant)+ stock_level = fulfillment.stock_location.stock_level(variant)expect { subject.call(fulfillment: fulfillment) }.- to change { stock_item.reload.count_on_hand }.by(+ to change { stock_level.reload.count_on_hand }.by(-fulfillment.fulfillment_items.where(variant_id: variant.id).sum(:quantity))expect(fulfillment.reload).to be_fulfilled@@ -167,10 +167,10 @@ module Spreeit 'does not unstock when the fulfillment was open' doSpree.fulfillment_resume_workflow.call(fulfillment: fulfillment)variant = fulfillment.fulfillment_items.first.variant- stock_item = fulfillment.stock_location.stock_item(variant)+ stock_level = fulfillment.stock_location.stock_level(variant)expect { subject.call(fulfillment: fulfillment) }.- not_to change { stock_item.reload.count_on_hand }+ not_to change { stock_level.reload.count_on_hand }endenddiff --git a/spree/core/spec/workflows/spree/fulfillments/resume_spec.rb b/spree/core/spec/workflows/spree/fulfillments/resume_spec.rbindex 41cacf654b..bef0f7a1de 100644--- a/spree/core/spec/workflows/spree/fulfillments/resume_spec.rb+++ b/spree/core/spec/workflows/spree/fulfillments/resume_spec.rb@@ -19,9 +19,9 @@ module Spreeit 'takes the units back off the shelf' dovariant = fulfillment.fulfillment_items.first.variant- stock_item = fulfillment.stock_location.stock_item(variant)+ stock_level = fulfillment.stock_location.stock_level(variant)- expect { execute }.to change { stock_item.reload.count_on_hand }.by(+ expect { execute }.to change { stock_level.reload.count_on_hand }.by(-fulfillment.fulfillment_items.where(variant_id: variant.id).sum(:quantity))end@@ -43,9 +43,9 @@ module SpreeSpree.hooks.register('fulfillments.resume.validate') { |flow| flow.reject!('stock sold on') }variant = fulfillment.fulfillment_items.first.variant- stock_item = fulfillment.stock_location.stock_item(variant)+ stock_level = fulfillment.stock_location.stock_level(variant)- expect { execute }.not_to change { stock_item.reload.count_on_hand }+ expect { execute }.not_to change { stock_level.reload.count_on_hand }expect(fulfillment.reload).to be_canceledenddiff --git a/spree/core/spec/workflows/spree/orders/add_item_spec.rb b/spree/core/spec/workflows/spree/orders/add_item_spec.rbindex e691de6877..b4e21d69cf 100644--- a/spree/core/spec/workflows/spree/orders/add_item_spec.rb+++ b/spree/core/spec/workflows/spree/orders/add_item_spec.rb@@ -32,7 +32,7 @@ module Spreeendit 'fails when the variant has insufficient stock' do- variant.stock_items.first.update!(backorderable: false)+ variant.stock_levels.first.update!(backorderable: false)result = subject.call(order: order, variant: variant, quantity: 10)diff --git a/spree/core/spec/workflows/spree/orders/cancel_spec.rb b/spree/core/spec/workflows/spree/orders/cancel_spec.rbindex c3df9a213b..905c714b2c 100644--- a/spree/core/spec/workflows/spree/orders/cancel_spec.rb+++ b/spree/core/spec/workflows/spree/orders/cancel_spec.rb@@ -31,10 +31,10 @@ module Spreeit 'puts the units back on the shelf' dofulfillment = order.fulfillments.firstvariant = fulfillment.fulfillment_items.first.variant- stock_item = fulfillment.stock_location.stock_item(variant)+ stock_level = fulfillment.stock_location.stock_level(variant)expect { subject.call(order: order, canceler: user) }.- to change { stock_item.reload.count_on_hand }.by(+ to change { stock_level.reload.count_on_hand }.by(fulfillment.fulfillment_items.where(variant_id: variant.id).sum(:quantity))enddiff --git a/spree/core/spec/workflows/spree/orders/upsert_items_spec.rb b/spree/core/spec/workflows/spree/orders/upsert_items_spec.rbindex 863535d556..843770cf4d 100644--- a/spree/core/spec/workflows/spree/orders/upsert_items_spec.rb+++ b/spree/core/spec/workflows/spree/orders/upsert_items_spec.rb@@ -10,7 +10,7 @@ module Spreebefore do[variant, variant2].each do |v|- v.stock_items.first.update!(count_on_hand: 10)+ v.stock_levels.first.update!(count_on_hand: 10)store.products << v.product unless store.products.include?(v.product)endend@@ -218,7 +218,7 @@ module Spreelet(:other_variant) { create(:variant) }before do- other_variant.stock_items.first.update!(count_on_hand: 10)+ other_variant.stock_levels.first.update!(count_on_hand: 10)other_store.products << other_variant.productenddiff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rbindex 638b03a4b3..ebe05df0ee 100644--- a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb+++ b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb@@ -99,16 +99,16 @@ RSpec.describe 'Spree::Returns workflows' doit 'restocks resellable goods' dovariant = line.variant- stock_item = return_record.stock_location.stock_item_or_create(variant)+ stock_level = return_record.stock_location.stock_level_or_create(variant)expect { Spree::Returns::Receive.call(return_record: return_record) }.- to change { stock_item.reload.count_on_hand }.by(line.quantity)+ to change { stock_level.reload.count_on_hand }.by(line.quantity)end# The whole reason receiving is a workflow and not a state transition.it 'records a partial, non-resellable receipt without restocking' dovariant = line.variant- stock_item = return_record.stock_location.stock_item_or_create(variant)+ stock_level = return_record.stock_location.stock_level_or_create(variant)result = Spree::Returns::Receive.call(return_record: return_record,@@ -118,7 +118,7 @@ RSpec.describe 'Spree::Returns workflows' doexpect(result).to be_successexpect(line.reload.received_quantity).to eq(1)expect(line.resellable).to be(false)- expect(stock_item.reload.count_on_hand).to eq(stock_item.count_on_hand)+ expect(stock_level.reload.count_on_hand).to eq(stock_level.count_on_hand)endit 'refuses to receive more than was requested' do