Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.md
index 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.rb
index 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 Spree
module StockLocations
module 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.')
+ super
end
end
end
diff --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.rb
new file mode 100644
index 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
+end
diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rb
index 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 Spree
where(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_levels
joins("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 Spree
if in_stock == '0' || !in_stock
all
else
- join_variants_and_stock_items.in_stock_or_backorderable_condition
+ join_variants_and_stock_levels.in_stock_or_backorderable_condition
end
end
@@ -87,16 +87,16 @@ module Spree
if out_of_stock == '0' || !out_of_stock
all
else
- 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)
end
end
def self.backorderable
- join_variants_and_stock_items.where(StockItem.table_name => { backorderable: true })
+ join_variants_and_stock_levels.where(StockLevel.table_name => { backorderable: true })
end
def self.in_stock_or_backorderable
- join_variants_and_stock_items.in_stock_or_backorderable_condition
+ join_variants_and_stock_levels.in_stock_or_backorderable_condition
end
# This scope selects products in taxon AND all its descendants
diff --git a/spree/core/app/models/spree/exports/products.rb b/spree/core/app/models/spree/exports/products.rb
index 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 Spree
end
def variant_includes
- [:images, :prices, :stock_items, :stock_locations, { option_values: [:option_type] }]
+ [:images, :prices, :stock_levels, :stock_locations, { option_values: [:option_type] }]
end
def multi_line_csv?
diff --git a/spree/core/app/models/spree/fulfillment_item.rb b/spree/core/app/models/spree/fulfillment_item.rb
index 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 Spree
scope :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) do
includes(: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_location
end
end
@@ -70,8 +70,8 @@ module Spree
update_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)
end
def self.split(original_inventory_unit, extract_quantity)
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index 6cf75c6596..d028f9f893 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -586,8 +586,8 @@ module Spree
def backordered_variants
variants.
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 })
end
def 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.rb
index 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 Spree
class MinimizeSplits < Spree::OrderRoutingRule
def 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 Spree
end
# 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.rb
index 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 Spree
def eligible_locations
order.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_a
end
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index ef7b9760d9..3208296f38 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -160,7 +160,7 @@ module Spree
has_many :prices, through: :variants
- has_many :stock_items, through: :variants
+ has_many :stock_levels, through: :variants
has_many :line_items, through: :variants
has_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 Spree
if 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)
end
end
end
+ # @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 master
Spree::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.rb
index 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 Spree
private
def 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 }).distinct
end
# An item may only be allocated from locations its delivery profile
diff --git a/spree/core/app/models/spree/stock/quantifier.rb b/spree/core/app/models/spree/stock/quantifier.rb
index 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 reservations
def available_stock
if 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)
end
end
@@ -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 Spree
def reserved_quantity
return @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_id
excluded_owner_id = excluded_order&.id
@reserved_quantity = if reservations_preloaded?
- stock_items.sum do |si|
+ stock_levels.sum do |si|
reservations = si.active_stock_reservations
reservations = reservations.reject { |r| r.public_send(excluded_owner_key) == excluded_owner_id } if excluded_owner_id
reservations.sum(&:quantity)
end
else
- 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_id
reservations.sum(:quantity)
end
@@ -87,7 +87,7 @@ module Spree
# Check if any of variant stock items is backorderable
def 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 Spree
supplyable >= required
end
- 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 Spree
def 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)
end
private
@@ -140,12 +140,12 @@ module Spree
end
def association_loaded?
- variant.association(:stock_items).loaded?
+ variant.association(:stock_levels).loaded?
end
def reservations_preloaded?
association_loaded? &&
- stock_items.all? { |si| si.association(:active_stock_reservations).loaded? }
+ stock_levels.all? { |si| si.association(:active_stock_reservations).loaded? }
end
def scope_to_location(collection)
diff --git a/spree/core/app/models/spree/stock_level.rb b/spree/core/app/models/spree/stock_level.rb
index 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 Spree
publishes_lifecycle_events
- with_options inverse_of: :stock_items do
+ with_options inverse_of: :stock_levels do
belongs_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)
end
def adjust_count_on_hand(value)
diff --git a/spree/core/app/models/spree/stock_location.rb b/spree/core/app/models/spree/stock_location.rb
index 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 Spree
has_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_levels
has_iso_geography
@@ -42,7 +42,7 @@ module Spree
scope :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_default
after_update :conditional_touch_records
@@ -54,35 +54,59 @@ module Spree
# Wrapper for creating a new stock item respecting the backorderable config
def 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).first
end
def 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.id
variant = variant_or_variant_id
@@ -90,7 +114,7 @@ module Spree
variant_id = variant_or_variant_id
variant = 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)
end
def backorderable?(variant)
- stock_item(variant).try(:backorderable?)
+ stock_level(variant).try(:backorderable?)
end
def restock(variant, quantity, originator = nil, persist: true)
@@ -111,7 +135,7 @@ module Spree
end
def 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 Spree
end
def 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)
end
end
def fill_status(variant, quantity)
- if item = stock_item_or_create(variant)
+ if item = stock_level_or_create(variant)
if item.count_on_hand >= quantity
on_hand = quantity
backordered = 0
@@ -190,8 +214,8 @@ module Spree
private
- 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 Spree
def conditional_touch_records
return 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)
end
end
diff --git a/spree/core/app/models/spree/stock_transfer.rb b/spree/core/app/models/spree/stock_transfer.rb
index 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 Spree
has_many :stock_movements, as: :originator
accepts_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 Spree
private
def 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)
end
def source_location_is_not_destination_location
@@ -92,7 +92,7 @@ module Spree
def 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.size
end
end
end
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index 917c748119..961d44e2f8 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -133,7 +133,7 @@ module Spree
has_many :products, class_name: 'Spree::Product', dependent: :nullify
has_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: :store
has_many :fulfillment_items, through: :variants, class_name: 'Spree::FulfillmentItem'
@@ -163,6 +163,12 @@ module Spree
self.categories = value
end
+ # @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: :destroy
has_many :delivery_methods, class_name: 'Spree::DeliveryMethod', dependent: :nullify
has_many :delivery_profiles, class_name: 'Spree::DeliveryProfile', dependent: :destroy
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index fa6d5e974b..38f0eb2ae1 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -36,12 +36,12 @@ module Spree
with_options inverse_of: :variant do
has_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: true
end
has_many :inventory_units, class_name: 'Spree::FulfillmentItem', inverse_of: :variant, deprecated: true
has_many :orders, through: :line_items
- with_options through: :stock_items do
+ with_options through: :stock_levels do
has_many :stock_locations
has_many :stock_movements
has_many :stock_reservations
@@ -91,17 +91,17 @@ module Spree
validates :hs_code, format: { with: /\A\d{6,13}\z/ }, allow_blank: true
validates :country_of_origin, format: { with: /\A[A-Z]{2}\z/ }, allow_blank: true
- after_create :create_stock_items
+ after_create :create_stock_levels
after_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_change
after_create :increment_product_variant_count
after_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 Spree
end
accepts_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_access
location = Spree::StockLocation.find_by_param(stock_data[:stock_location_id])
location_ids_in_payload << location.id
set_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 = value
end
# 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?
end
def default_stock_location
@@ -712,7 +730,7 @@ module Spree
end
def 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_levels
locations = product&.store ? product.store.stock_locations : StockLocation.all
locations.where(propagate_all_variants: true).each do |stock_location|
stock_location.propagate_variant(self)
@@ -781,17 +799,17 @@ module Spree
line_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)
end
def handle_track_inventory_change
return 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)
end
def increment_product_variant_count
diff --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.rb
index 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 Spree
return 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 = nil
diff --git a/spree/core/app/services/spree/products/duplicator.rb b/spree/core/app/services/spree/products/duplicator.rb
index 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 Spree
new_variant.deleted_at = nil
new_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 Spree
end
end
- 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_level
end
end
diff --git a/spree/core/app/services/spree/stock_locations/stock_items/create.rb b/spree/core/app/services/spree/stock_locations/stock_items/create.rb
index 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 Spree
module StockLocations
module 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 Create
prepend 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)
end
end
end
diff --git a/spree/core/app/services/spree/stock_locations/stock_levels/create.rb b/spree/core/app/services/spree/stock_locations/stock_levels/create.rb
new file mode 100644
index 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
+end
diff --git a/spree/core/app/services/spree/stock_reservations/reserve.rb b/spree/core/app/services/spree/stock_reservations/reserve.rb
index 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.quantity
raise 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 = cart
reservation.order = nil
@@ -70,47 +70,47 @@ module Spree
private
def 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.variant
next 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]
end
end
- 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_id
Spree::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] }
end
end
end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 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: State
other: States
- spree/stock_item:
- one: Stock Item
- other: Stock Items
+ spree/stock_level:
+ one: Stock Level
+ other: Stock Levels
spree/stock_location:
one: Stock Location
other: Stock Locations
@@ -1551,7 +1551,7 @@ en:
states_required: States Required
status: Status
stock: Stock
- stock_items: Stock Items
+ stock_levels: Stock Levels
stock_location: Stock Location
stock_locations: Stock Locations
stock_movements: Stock Movements
diff --git a/spree/core/lib/spree/core.rb b/spree/core/lib/spree/core.rb
index 18ab7632db..6b6336101e 100644
--- a/spree/core/lib/spree/core.rb
+++ b/spree/core/lib/spree/core.rb
@@ -118,7 +118,7 @@ module Spree
variants: :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.rb
index 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 Spree
variant_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.rb
index 637eea7bbf..bc6bc95248 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -447,7 +447,7 @@ module Spree
Spree::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.rb
index 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.rb
index 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.rb
index 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 do
order.line_items.reload
end
- 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.reload
diff --git a/spree/core/lib/spree/testing_support/factories/product_factory.rb b/spree/core/lib/spree/testing_support/factories/product_factory.rb
index 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 do
create(:stock_location) unless Spree::StockLocation.any?
end
after(: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 do
factory :product_in_stock do
after :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)
end
trait :without_backorder do
after :create do |product|
- product.default_variant.stock_items.update_all(backorderable: false)
+ product.default_variant.stock_levels.update_all(backorderable: false)
end
end
end
diff --git a/spree/core/lib/spree/testing_support/factories/stock_location_factory.rb b/spree/core/lib/spree/testing_support/factories/stock_location_factory.rb
index 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 do
product_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)
end
end
end
diff --git a/spree/core/lib/spree/testing_support/factories/stock_movement_factory.rb b/spree/core/lib/spree/testing_support/factories/stock_movement_factory.rb
index 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 do
factory :stock_movement, class: Spree::StockMovement do
quantity { 1 }
action { 'sold' }
- stock_item
+ stock_level
end
trait :received do
diff --git a/spree/core/lib/spree/testing_support/factories/stock_reservation_factory.rb b/spree/core/lib/spree/testing_support/factories/stock_reservation_factory.rb
index 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 do
end
# 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 do
owner.line_items.reload
end
- 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)
end
trait :expired do
diff --git a/spree/core/lib/spree/testing_support/factories/variant_factory.rb b/spree/core/lib/spree/testing_support/factories/variant_factory.rb
index 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 do
after(: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)
end
diff --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.rb
index 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 Spree
describe StockLocations::StockItems::CreateJob, :job do
let!(: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' do
expect { 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)
+ end
end
end
diff --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.rb
new file mode 100644
index 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
+end
diff --git a/spree/core/spec/models/spree/cart_spec.rb b/spree/core/spec/models/spree/cart_spec.rb
index 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 do
context 'when an item is out of stock' do
before 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)
end
it 'removes the out of stock item' do
diff --git a/spree/core/spec/models/spree/event_spec.rb b/spree/core/spec/models/spree/event_spec.rb
index 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 do
end
it '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')
end
end
@@ -144,7 +144,7 @@ RSpec.describe Spree::Event do
end
it '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')
end
end
diff --git a/spree/core/spec/models/spree/fulfilment_changer_spec.rb b/spree/core/spec/models/spree/fulfilment_changer_spec.rb
index 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 do
before do
order && desired_shipment
- variant.stock_items.first.update_column(:count_on_hand, 100)
+ variant.stock_levels.first.update_column(:count_on_hand, 100)
end
context 'when the order has multiple line items' do
@@ -113,11 +113,11 @@ describe Spree::FulfilmentChanger do
context 'when transferring to another stock location' do
let(: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)
end
context 'when the other stock location has enough stock' do
@@ -145,7 +145,7 @@ describe Spree::FulfilmentChanger do
end
it '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)
end
end
end
@@ -156,7 +156,7 @@ describe Spree::FulfilmentChanger do
let(:desired_count_on_hand) { 5 }
before do
- stock_item.update(backorderable: true)
+ stock_level.update(backorderable: true)
end
it 'restocks seven at the original stock location' do
@@ -241,21 +241,21 @@ describe Spree::FulfilmentChanger do
end
context '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 do
current_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)
end
it '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)
end
it '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)
end
it 'creates a shipment with the correct number of on hand and backordered units' do
@@ -404,9 +404,9 @@ describe Spree::FulfilmentChanger do
end
end
- context 'when stock_item is last on_hand' do
+ context 'when stock_level is last on_hand' do
before do
- variant.stock_items.first.update_column(:count_on_hand, 1)
+ variant.stock_levels.first.update_column(:count_on_hand, 1)
end
let(: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.rb
index 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 do
let(: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' do
let!(:inventory_unit_1) { create(:inventory_unit, state: 'on_hand') }
@@ -31,13 +31,13 @@ describe Spree::InventoryUnit, type: :model do
end
end
- describe '#backordered_for_stock_item' do
+ describe '#backordered_for_stock_level' do
let(:order) do
order = create(:order, state: 'complete', ship_address: create(:ship_address))
order.completed_at = Time.current
create(: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.reload
order.tap(&:save!)
end
@@ -57,17 +57,17 @@ describe Spree::InventoryUnit, type: :model do
end
before do
- stock_item.set_count_on_hand(-2)
+ stock_level.set_count_on_hand(-2)
end
# Regression for #3066
it '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_error
end
it "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])
end
it "does not find inventory units that aren't backordered" do
@@ -76,7 +76,7 @@ describe Spree::InventoryUnit, type: :model do
on_hand_unit.variant_id = 1
on_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)
end
it "does not find inventory units that don't match the stock item's variant" do
@@ -85,13 +85,13 @@ describe Spree::InventoryUnit, type: :model do
other_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)
end
it 'does not change shipping cost when fulfilling the order' do
current_shipment_cost = shipment.cost
shipping_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 do
let!(:other_unit) do
unit = other_shipment.inventory_units.build
unit.state = 'backordered'
- unit.variant_id = stock_item.variant.id
+ unit.variant_id = stock_level.variant.id
unit.order_id = other_order.id
unit.tap(&:save!)
end
it '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)
end
end
end
diff --git a/spree/core/spec/models/spree/line_item_spec.rb b/spree/core/spec/models/spree/line_item_spec.rb
index 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 do
it 'decrements stock for a tracked digital variant' do
second_licence = create(:product, delivery_profile: digital_profile, store: store).default_variant
second_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)
end
end
@@ -411,7 +411,7 @@ describe Spree::LineItem, type: :model do
context 'nothing left on stock' do
before do
- variant.stock_items.update_all count_on_hand: 5, backorderable: false
+ variant.stock_levels.update_all count_on_hand: 5, backorderable: false
Spree::Orders::AddItem.call(order: order, variant: variant, quantity: 5)
order.rebuild_fulfillments!
order.finalize!
@@ -440,7 +440,7 @@ describe Spree::LineItem, type: :model do
context '2 items left on stock' do
before do
- variant.stock_items.update_all count_on_hand: 7, backorderable: false
+ variant.stock_levels.update_all count_on_hand: 7, backorderable: false
Spree::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.rb
index 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 do
before { stub_store_preferences(track_inventory_levels: false) }
it 'creates only on hand inventory units' do
- variant.stock_items.delete_all
+ variant.stock_levels.delete_all
expect_any_instance_of(Spree::StockLocation).not_to receive(:unstock)
@@ -66,7 +66,7 @@ describe Spree::OrderInventory, type: :model do
before { variant.update(track_inventory: false) }
it 'creates only on hand inventory units' do
- variant.stock_items.delete_all
+ variant.stock_levels.delete_all
expect_any_instance_of(Spree::StockLocation).not_to receive(:unstock)
@@ -82,8 +82,8 @@ describe Spree::OrderInventory, type: :model do
it 'creates stock_movement' do
expect(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 == shipment
expect(movement.quantity).to eq(-5)
end
@@ -234,7 +234,7 @@ describe Spree::OrderInventory, type: :model do
it 'doesn\'t create on_hand items from backordered items' do
shipment.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)
end
end
end
diff --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.rb
index 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 do
let(: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.
end
diff --git a/spree/core/spec/models/spree/order_routing/strategy/parity_spec.rb b/spree/core/spec/models/spree/order_routing/strategy/parity_spec.rb
index 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 do
o.reload
end
- 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' do
units = location_units(strategy.for_allocation)
@@ -63,8 +63,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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)
end
it 'NYC covers everything, LA never engaged' do
@@ -87,8 +87,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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)
end
it 'splits variant_a → NYC, variant_b → LA' do
@@ -111,9 +111,9 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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)
end
it 'splits across all three' do
@@ -136,8 +136,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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 do
end
before 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)
end
it 'must split across both locations to avoid backorder' do
@@ -183,8 +183,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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)
end
it 'allocates available stock plus backorders the rest' do
@@ -207,9 +207,9 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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)
end
it '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 do
end
before 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)
end
it 'returns a single backordered package' do
@@ -241,7 +241,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
end
- context 'variant has stock_items but zero count, backorderable everywhere' do
+ context 'variant has stock_levels but zero count, backorderable everywhere' do
let(:variant_unstocked) { create(:variant) }
let(:order) do
@@ -251,7 +251,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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)
end
it 'returns a single backordered package' do
@@ -305,13 +305,13 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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 do
end
before 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 do
end
before 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 do
o.reload
end
- 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' do
packages = strategy.for_allocation
@@ -422,8 +422,8 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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 do
context 'one location inactive, the other active' do
before 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' do
let(:order) do
o = 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 do
end
before 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)
end
it '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.reload
end
@@ -536,7 +536,7 @@ RSpec.describe 'OrderRouting strategy parity', type: :model do
end
before 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 do
end
before 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 do
end
before 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)
end
it 'backorders all units at a single location' do
diff --git a/spree/core/spec/models/spree/order_routing/strategy/rules_spec.rb b/spree/core/spec/models/spree/order_routing/strategy/rules_spec.rb
index 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 do
before 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)
end
end
diff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rb
index 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 do
before do
create(: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)
end
it 'returns the backordered variants' do
@@ -2310,7 +2310,7 @@ describe Spree::Order, type: :model do
context 'when an item is out of stock' do
before 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)
end
it 'removes the out of stock item' do
diff --git a/spree/core/spec/models/spree/product/scopes_spec.rb b/spree/core/spec/models/spree/product/scopes_spec.rb
index 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 do
describe '#in_stock / #out_of_stock' do
let!(:in_stock_product) do
create(: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)
end
end
let!(:backorderable_product) do
create(: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)
end
end
let!(:out_of_stock_product) do
create(: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)
end
end
diff --git a/spree/core/spec/models/spree/product_preorder_spec.rb b/spree/core/spec/models/spree/product_preorder_spec.rb
index 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 do
let(: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 quantifier
Spree::Stock::Quantifier.new(variant)
@@ -51,7 +51,7 @@ RSpec.describe 'Pre-order', type: :model do
describe 'pre-order does not bypass the product status' do
it 'cannot supply an archived product even when the variant is preorderable' do
variant.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 false
end
@@ -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' do
before 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)
end
context 'pre-order with no backorder_limit (empty = unlimited)' do
@@ -90,13 +90,13 @@ RSpec.describe 'Pre-order', type: :model do
end
it '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 true
expect(quantifier.can_supply?(8)).to be false
end
it '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 oversold
expect(quantifier.can_supply?(1)).to be false
expect(variant.purchasable?).to be false
end
@@ -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' do
variant.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 do
variant.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)
end
it 'backorders the shortfall for a pre-order variant' do
@@ -167,8 +167,8 @@ RSpec.describe 'Pre-order', type: :model do
before do
variant.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 published
product.product_publications.reset
end
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index 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)
end
end
@@ -72,9 +72,9 @@ describe Spree::Product, type: :model do
all_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)
end
expect(product.reload.send("#{method_name}?")).to eq true
@@ -244,7 +244,7 @@ describe Spree::Product, type: :model do
end
it '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)
end
end
@@ -262,10 +262,10 @@ describe Spree::Product, type: :model do
context 'has stock movements' do
let(: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_error
end
end
@@ -273,8 +273,8 @@ describe Spree::Product, type: :model do
# Regression test for #3737
context 'has stock items' do
it '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_nil
end
end
@@ -335,7 +335,7 @@ describe Spree::Product, type: :model do
let(: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_hand
product.instance_variable_set(:@total_on_hand, incorrent_total_on_hand)
end
@@ -462,9 +462,9 @@ describe Spree::Product, type: :model do
it 'creates a default stock item' do
product.save
expect(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)
end
end
@@ -687,13 +687,13 @@ describe Spree::Product, type: :model do
end
it '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 5
product.variants.reload # force load association
expect(product.total_on_hand).to be(5)
end
it '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 5
expect(product.reload.total_on_hand).to be(5)
end
end
@@ -751,12 +751,12 @@ describe Spree::Product, type: :model do
end
it '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)
end
it '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)
end
end
@@ -1236,7 +1236,7 @@ describe Spree::Product, type: :model do
subject { 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' do
it { expect(subject).to eq(true) }
@@ -1247,21 +1247,21 @@ describe Spree::Product, type: :model do
let!(:variant) { create(:variant, product: product) }
before do
- Spree::StockItem.update_all(backorderable: false)
+ Spree::StockLevel.update_all(backorderable: false)
product.reload
end
context 'with at least one option variant stock items count_on_hand > 0' do
before 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) }
end
it { expect(subject).to eq(true) }
end
context '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 do
it { 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 do
let!(: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.reload
end
diff --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.rb
index 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 do
let(: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)
end
context '#perform' do
diff --git a/spree/core/spec/models/spree/shipment_spec.rb b/spree/core/spec/models/spree/shipment_spec.rb
index 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 do
let(: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_method
shipping_method.calculator.preferences[:amount] = order.fulfillments.first.cost
shipping_method.calculator.save!
diff --git a/spree/core/spec/models/spree/stock/coordinator_spec.rb b/spree/core/spec/models/spree/stock/coordinator_spec.rb
index 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 Spree
let!(: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.order
end
diff --git a/spree/core/spec/models/spree/stock/quantifier_spec.rb b/spree/core/spec/models/spree/stock/quantifier_spec.rb
index 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 @@ end
module Spree
module Stock
describe 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 database
let!(: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)
end
context 'when variant is available' do
@@ -40,7 +40,7 @@ module Spree
end
context '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 Spree
end
context '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 Spree
let!(: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' do
expect(subject.total_on_hand).to eq(15)
end
@@ -101,7 +101,7 @@ module Spree
end
context '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 Spree
end
context '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' do
before { stub_store_preferences(stock_reservations_enabled: true) }
@@ -134,32 +134,32 @@ module Spree
it 'subtracts active reservations from total_on_hand' do
create(
: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)
end
it 'ignores expired reservations' do
create(
: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)
end
it '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 Spree
it 'reserved_quantity returns the sum of active reservations' do
create(
: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 Spree
end
context '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" do
create(
: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)
end
it "still counts other orders' reservations" do
create(
: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 Spree
end
context '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' do
create(
: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 Spree
it 'returns raw count_on_hand even when reservations exist' do
create(
: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)
end
end
diff --git a/spree/core/spec/models/spree/stock_level_spec.rb b/spree/core/spec/models/spree/stock_level_spec.rb
index 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 do
it 'recreates stock item just fine' do
expect do
- stock_location.stock_items.create!(variant: subject.variant)
+ stock_location.stock_levels.create!(variant: subject.variant)
end.not_to raise_error
end
it '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)
end
end
@@ -540,18 +540,18 @@ describe Spree::StockLevel, type: :model do
describe 'scopes' do
context '.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' do
before { 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) }
end
context 'when stock location is inactive' do
before { 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) }
end
end
@@ -562,10 +562,10 @@ describe Spree::StockLevel, type: :model do
let(: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)
end
let!(: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)
end
it 'returns stock items for variants of products owned by the store' do
diff --git a/spree/core/spec/models/spree/stock_location_spec.rb b/spree/core/spec/models/spree/stock_location_spec.rb
index 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 Spree
describe StockLocation, type: :model do
subject { 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' do
let!(:variant) { create(:variant) }
before do
- Spree::StockItem.delete_all
+ Spree::StockLevel.delete_all
described_class.delete_all
end
- it 'creates stock_items for all variants' do
+ it 'creates stock_levels for all variants' do
expect do
perform_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 Spree
context 'set up' do
it 'creates stock item' do
expect(subject).to receive(:propagate_variant)
- subject.set_up_stock_item(variant)
+ subject.set_up_stock_level(variant)
end
context '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)
end
end
end
context 'propagate variants' do
- let(:stock_item) { subject.propagate_variant(variant) }
+ let(:stock_level) { subject.propagate_variant(variant) }
it 'creates a new stock item' do
expect do
subject.propagate_variant(variant)
- end.to change(StockItem, :count).by(1)
+ end.to change(StockLevel, :count).by(1)
end
context 'passes backorderable default config' do
context 'true' do
before { subject.backorderable_default = true }
- it { expect(stock_item.backorderable).to be true }
+ it { expect(stock_level.backorderable).to be true }
end
context 'false' do
before { subject.backorderable_default = false }
- it { expect(stock_item.backorderable).to be false }
+ it { expect(stock_level.backorderable).to be false }
end
end
end
@@ -85,8 +85,8 @@ module Spree
before { 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 Spree
before { 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!
end
end
@@ -105,59 +105,59 @@ module Spree
end
end
- 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 10
end
- 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 variant
end
- it 'returns nil when stock_item is not found for variant' do
+ it 'returns nil when stock_level is not found for variant' do
variant_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_nil
end
- describe '#stock_item_or_create' do
+ describe '#stock_level_or_create' do
context 'without stock item' do
let!(: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 variant
end
- 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) }
end
context '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 variant
end
- 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) }
end
end
context 'with stock item' do
let!(: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) }
end
context '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) }
end
end
end
@@ -185,7 +185,7 @@ module Spree
it 'creates a stock_movement' do
expect do
subject.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)
end
it 'can be deactivated' do
@@ -209,9 +209,9 @@ module Spree
end
context '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' do
on_hand, backordered = subject.fill_status(variant, 5)
@@ -226,7 +226,7 @@ module Spree
end
it '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 Spree
context 'when backordering is not allowed' do
before 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)
end
it '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 Spree
end
it '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 Spree
end
it '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 Spree
end
end
- context 'without stock_items' do
+ context 'without stock_levels' do
subject { create(:stock_location) }
let(:variant) { create(:base_variant) }
it 'zero on_hand and one backordered' do
subject
- variant.stock_items.delete_all
+ variant.stock_levels.delete_all
on_hand, backordered = subject.fill_status(variant, 1)
expect(on_hand).to eq 0
expect(backordered).to eq 1
diff --git a/spree/core/spec/models/spree/stock_movement/custom_events_spec.rb b/spree/core/spec/models/spree/stock_movement/custom_events_spec.rb
index 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 do
let(: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)
item
end
@@ -19,24 +19,24 @@ RSpec.describe Spree::StockMovement::CustomEvents do
describe 'product.out_of_stock event' do
before do
- stock_item.set_count_on_hand(10)
+ stock_level.set_count_on_hand(10)
end
it '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)
end
it '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)
end
it '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 setup
RSpec::Mocks.space.proxy_for(Spree::Events).reset
@@ -45,7 +45,7 @@ RSpec.describe Spree::StockMovement::CustomEvents do
allow(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 do
describe 'product.back_in_stock event' do
before do
- stock_item.set_count_on_hand(0)
+ stock_level.set_count_on_hand(0)
end
it '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)
end
it '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 do
describe 'when events are disabled' do
before do
allow(Spree::Events).to receive(:enabled?).and_return(false)
- stock_item.set_count_on_hand(10)
+ stock_level.set_count_on_hand(10)
end
it '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)
end
diff --git a/spree/core/spec/models/spree/stock_movement_spec.rb b/spree/core/spec/models/spree/stock_movement_spec.rb
index 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 do
describe 'validations' do
it "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_invalid
expect(stock_movement.errors[:quantity]).to include('must be greater than or equal to -10')
end
it '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_valid
end
@@ -62,19 +62,19 @@ describe Spree::StockMovement, type: :model do
describe 'Instance Methods' do
let(: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) }
end
describe '.variant' do
- it { expect(stock_movement.variant).to eq(stock_item.variant) }
+ it { expect(stock_movement.variant).to eq(stock_level.variant) }
end
describe '#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' do
expect { stock_movement.save }.to raise_error(ActiveRecord::ReadOnlyRecord)
@@ -87,24 +87,24 @@ describe Spree::StockMovement, type: :model do
stub_store_preferences(track_inventory_levels: false)
stock_movement.quantity = 1
stock_movement.save
- stock_item.reload
+ stock_level.reload
end
it '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)
end
end
context 'when track inventory tracking is off' do
before do
- stock_item.variant.track_inventory = false
+ stock_level.variant.track_inventory = false
stock_movement.quantity = 1
stock_movement.save
- stock_item.reload
+ stock_level.reload
end
it '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)
end
end
@@ -112,11 +112,11 @@ describe Spree::StockMovement, type: :model do
before do
stock_movement.quantity = -1
stock_movement.save
- stock_item.reload
+ stock_level.reload
end
it '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)
end
end
@@ -124,11 +124,11 @@ describe Spree::StockMovement, type: :model do
before do
stock_movement.quantity = 1
stock_movement.save
- stock_item.reload
+ stock_level.reload
end
it '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)
end
end
end
diff --git a/spree/core/spec/models/spree/stock_transfer_spec.rb b/spree/core/spec/models/spree/stock_transfer_spec.rb
index 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 Spree
let(: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.rb
index 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 do
it { 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) }
end
end
end
diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rb
index 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 do
end
- describe '#create_default_stock_item' do
+ describe '#create_default_stock_level' do
let(:new_variant) { product.variants.create(track_inventory: track_inventory) }
context 'when not tracking inventory' do
@@ -135,16 +135,16 @@ describe Spree::Variant, type: :model do
it 'creates a default stock item' do
new_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)
end
context 'when variant is created along with a stock item' do
let(:new_variant) do
product.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 do
it 'does not create an another stock item' do
new_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)
end
end
end
@@ -169,7 +169,7 @@ describe Spree::Variant, type: :model do
it 'does not create a default stock item' do
new_variant
- expect(new_variant.reload.stock_items.count).to eq(0)
+ expect(new_variant.reload.stock_levels.count).to eq(0)
end
end
@@ -178,11 +178,11 @@ describe Spree::Variant, type: :model do
# clear out previous stock items
before do
- variant.stock_items.delete_all
+ variant.stock_levels.delete_all
end
it '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)
end
end
end
@@ -195,10 +195,10 @@ describe Spree::Variant, type: :model do
subject { 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)
end
end
@@ -206,10 +206,10 @@ describe Spree::Variant, type: :model do
subject { 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)
end
end
end
@@ -491,7 +491,7 @@ describe Spree::Variant, type: :model do
let!(: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 do
context 'when variant has stock items' do
let!(: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) }
end
context '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 do
it { 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 do
stub_store_preferences(track_inventory_levels: true)
end
- context 'when stock_items are not backorderable' do
+ context 'when stock_levels are not backorderable' do
before 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' do
before 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' do
expect(variant.in_stock?).to be true
end
end
- context 'when stock_items out of stock' do
+ context 'when stock_levels out of stock' do
before 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' do
expect(variant.in_stock?).to be false
end
end
@@ -947,14 +947,14 @@ describe Spree::Variant, type: :model do
end
end
- context 'when stock_items are backorderable' do
+ context 'when stock_levels are backorderable' do
before 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' do
before 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 'in_stock? returns false' do
@@ -980,53 +980,53 @@ describe Spree::Variant, type: :model do
end
describe '#purchasable?' do
- context 'when stock_items are not backorderable' do
+ context 'when stock_levels are not backorderable' do
before 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' do
before 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' do
expect(variant.purchasable?).to be true
end
end
- context 'when stock_items out of stock' do
+ context 'when stock_levels out of stock' do
before 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' do
expect(variant.purchasable?).to be false
end
end
end
- context 'when stock_items are out of stock' do
+ context 'when stock_levels are out of stock' do
before 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
context 'when stock item are backorderable' do
before 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' do
expect(variant.purchasable?).to be true
end
end
- context 'when stock_items are not backorderable' do
+ context 'when stock_levels are not backorderable' do
before 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' do
expect(variant.purchasable?).to be false
end
end
@@ -1164,7 +1164,7 @@ describe Spree::Variant, type: :model do
end
describe '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' do
expect(variant.stock_movements.to_a).not_to be_empty
@@ -1176,7 +1176,7 @@ describe Spree::Variant, type: :model do
in_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 do
end
it '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)
end
it '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)
end
end
@@ -1502,27 +1502,27 @@ describe Spree::Variant, type: :model do
end
end
- describe '#stock_items= (hash array)' do
+ describe '#stock_levels= (hash array)' do
let!(:stock_location) { Spree::StockLocation.first || create(:stock_location) }
let!(:stock_location_2) { create(:stock_location, name: 'Warehouse 2') }
context 'on persisted variant' do
it '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)
end
it '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 do
variant.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_nil
end
end
context 'on new variant' do
let(: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)
end
end
diff --git a/spree/core/spec/services/spree/carts/create_spec.rb b/spree/core/spec/services/spree/carts/create_spec.rb
index 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 Spree
let(: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 Spree
let(: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
diff --git a/spree/core/spec/services/spree/carts/destroy_spec.rb b/spree/core/spec/services/spree/carts/destroy_spec.rb
index 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 Spree
context 'with stock reservations' do
let(: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.rb
index 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 Spree
let(: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.rb
index 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 Spree
before do
cart.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)
end
end
@@ -63,7 +63,7 @@ module Spree
it 'reservation for the removed line item is destroyed via dependent: :destroy' do
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,
@@ -78,7 +78,7 @@ module Spree
it 'remaining line items get a fresh reservation pass' do
create(
: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 Spree
it '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.count
result = 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.rb
index 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 do
end
context '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 do
before do
stub_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.rb
index 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 Spree
context 'with non-backorderable item' do
before 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)
end
context 'with sufficient stock quantity' do
@@ -47,8 +47,8 @@ module Spree
context 'stock reservations' do
before 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)
end
context 'when the cart is mid-checkout' do
@@ -73,7 +73,7 @@ module Spree
it 'fails when the new quantity exceeds available stock and rolls back' do
subject.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.rb
index 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 Spree
let(: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 Spree
end
before 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)
end
context 'cart → checkout transition' do
@@ -799,11 +799,11 @@ module Spree
let(: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)
end
it 'returns failure and rolls back the cart update' do
diff --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.rb
index 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 do
expect(default_variant.sku).to be_blank
expect(default_variant.price_in('USD').amount.to_f).to eq 62.99
expect(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_location
end
it 'enqueues AssignTagsJob' do
@@ -92,22 +92,22 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
let!(: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!
end
it '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 false
subject.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 true
expect(existing_product.reload.name).to eq 'Denim Shirt'
end
@@ -242,22 +242,22 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
context 'when updating inventory values' do
before 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!
end
it '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 false
subject.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 true
end
end
end
diff --git a/spree/core/spec/services/spree/orders/create_spec.rb b/spree/core/spec/services/spree/orders/create_spec.rb
index 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) do
create(: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)
end
end
let(:params) do
diff --git a/spree/core/spec/services/spree/products/duplicator_spec.rb b/spree/core/spec/services/spree/products/duplicator_spec.rb
index 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 do
new_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)
end
it { is_expected.to be_success }
@@ -120,8 +120,8 @@ RSpec.describe Spree::Products::Duplicator do
let(: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)
end
it 'duplicates the variants' do
diff --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.rb
index 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 Spree
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) }
+ 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)
end
end
end
diff --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.rb
new file mode 100644
index 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
+end
diff --git a/spree/core/spec/services/spree/stock_reservations/reserve_spec.rb b/spree/core/spec/services/spree/stock_reservations/reserve_spec.rb
index 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 do
let(: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 do
expect(result).to be_success
reservation = 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 do
end
it '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_failure
expect(result.error.to_s).to include('available')
end
it '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 do
expect(result).to be_failure
end
- 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 do
end
it '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_success
end
diff --git a/spree/core/spec/workflows/spree/carts/add_item_spec.rb b/spree/core/spec/workflows/spree/carts/add_item_spec.rb
index 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 Spree
context 'variant does not have the desired quantity' do
let(: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' do
expect(execute).to be_failure
@@ -201,8 +201,8 @@ module Spree
before 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 Spree
before 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)
end
it 'is rejected because the product is not yet published' do
@@ -277,8 +277,8 @@ module Spree
let(: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)
end
context 'when the cart is mid-checkout' do
@@ -290,7 +290,7 @@ module Spree
end
it '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 Spree
before do
variant.update!(preorderable: true, backorder_limit: 5)
- variant.stock_items.first.set_count_on_hand(2)
+ variant.stock_levels.first.set_count_on_hand(2)
end
it 'skips reservation and accepts quantities beyond on-hand stock' do
diff --git a/spree/core/spec/workflows/spree/carts/complete_spec.rb b/spree/core/spec/workflows/spree/carts/complete_spec.rb
index 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 Spree
describe 'stock reservations' do
it 'releases reservations on successful completion' do
line_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)
end
diff --git a/spree/core/spec/workflows/spree/carts/upsert_items_spec.rb b/spree/core/spec/workflows/spree/carts/upsert_items_spec.rb
index 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 Spree
before 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)
end
end
@@ -167,7 +167,7 @@ module Spree
let(: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.product
end
@@ -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' do
workflow = described_class.new
diff --git a/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb b/spree/core/spec/workflows/spree/claims/lifecycle_spec.rb
index 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' do
it 'ignores the refund method when the resolution moves no money' do
replacing = 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' do
it 'ships a replacement' do
claim = 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.rb
index 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' do
it 'receives and restocks the returned variant' do
line = 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_received
end
it 'does not restock what came back damaged' do
line = 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)
end
end
@@ -97,7 +97,7 @@ RSpec.describe 'Spree::Exchanges workflows' do
it 'creates a replacement fulfillment and marks it fulfilled' do
exchange.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)
end
result = Spree::Exchanges::Fulfill.call(exchange: exchange)
@@ -112,7 +112,7 @@ RSpec.describe 'Spree::Exchanges workflows' do
context 'when the replacement is cheaper, so a credit is due' do
let(:cheap_exchange) do
exchange.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)
end
exchange
diff --git a/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb b/spree/core/spec/workflows/spree/fulfillments/cancel_spec.rb
index 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 Spree
it 'puts the units back on the shelf' do
variant = 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
diff --git a/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb b/spree/core/spec/workflows/spree/fulfillments/fulfill_spec.rb
index 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 Spree
it 'takes the units back off the shelf' do
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) }.
- 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 Spree
it 'does not unstock when the fulfillment was open' do
Spree.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 }
end
end
diff --git a/spree/core/spec/workflows/spree/fulfillments/resume_spec.rb b/spree/core/spec/workflows/spree/fulfillments/resume_spec.rb
index 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 Spree
it 'takes the units back off the shelf' do
variant = 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 Spree
Spree.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_canceled
end
diff --git a/spree/core/spec/workflows/spree/orders/add_item_spec.rb b/spree/core/spec/workflows/spree/orders/add_item_spec.rb
index 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 Spree
end
it '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.rb
index 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 Spree
it 'puts the units back on the shelf' do
fulfillment = order.fulfillments.first
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(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)
)
end
diff --git a/spree/core/spec/workflows/spree/orders/upsert_items_spec.rb b/spree/core/spec/workflows/spree/orders/upsert_items_spec.rb
index 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 Spree
before 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)
end
end
@@ -218,7 +218,7 @@ module Spree
let(: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.product
end
diff --git a/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb b/spree/core/spec/workflows/spree/returns/lifecycle_spec.rb
index 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' do
it 'restocks resellable goods' do
variant = 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' do
variant = 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' do
expect(result).to be_success
expect(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)
end
it 'refuses to receive more than was requested' do