Move the inventory feature check to ScopeVariantToHub

Per review, the check is done on the same enterprise as the one use to
initialize ScopeVariantToHub. So it makes sense to move the actual
feature check to ScopeVariantToHub#scope
This commit is contained in:
Gaetan Craig-Riou
2025-07-09 11:20:47 +10:00
parent b28e30cb6c
commit b7f969eed9
14 changed files with 30 additions and 69 deletions

View File

@@ -54,9 +54,7 @@ module Admin
fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new(@shop, @order_cycle)
OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(
@variant, inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @shop)
)
OpenFoodNetwork::ScopeVariantToHub.new(@shop).scope(@variant)
@variant.price + fee_calculator.indexed_fees_for(@variant)
end

View File

@@ -113,10 +113,7 @@ module Api
def scoped_variant(variant_id)
variant = Spree::Variant.find(variant_id)
OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(
variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @order.distributor)
)
OpenFoodNetwork::ScopeVariantToHub.new(@order.distributor).scope(variant)
variant
end

View File

@@ -290,12 +290,7 @@ class OrderCycle < ApplicationRecord
items = Spree::LineItem.includes(:variant).joins(:order).merge(orders)
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
items.each do |li|
scoper.scope(
li.variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor)
)
end
items.each { |li| scoper.scope(li.variant) }
items
end

View File

@@ -164,7 +164,7 @@ module Spree
return true if skip_stock_check
return true if quantity <= 0
scope_variant
scoper.scope(variant)
variant.can_supply?(quantity)
end
@@ -177,7 +177,7 @@ module Spree
end
def cap_quantity_at_stock!
scope_variant
scoper.scope(variant)
return if variant.on_demand
update!(quantity: variant.on_hand) if quantity > variant.on_hand
@@ -263,7 +263,7 @@ module Spree
def update_inventory
return unless changed?
scope_variant
scoper.scope(variant)
Spree::OrderInventory.new(order).verify(self, target_shipment)
end
@@ -294,14 +294,5 @@ module Spree
self.final_weight_volume = variant&.unit_value&.* quantity
end
end
def scope_variant
scoper.scope(
variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor)
)
variant
end
end
end

View File

@@ -195,10 +195,7 @@ module Spree
states = {}
units.group_by(&:state).each { |state, iu| states[state] = iu.count }
scoper.scope(
variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor)
)
scoper.scope(variant)
OpenStruct.new(variant:, quantity: units.length, states:)
end

View File

@@ -59,10 +59,7 @@ class CartService
end
def attempt_cart_add(variant, quantity, max_quantity = nil)
scoper.scope(
variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, order.distributor)
)
scoper.scope(variant)
return unless valid_variant?(variant)

View File

@@ -51,9 +51,7 @@ module Orders
attrs[:line_items].each do |li|
next unless variant = Spree::Variant.find_by(id: li[:variant_id])
scoper.scope(
variant, inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, shop)
)
scoper.scope(variant)
li[:quantity] = stock_limited_quantity(variant.on_demand, variant.on_hand, li[:quantity])
li[:price] = variant.price

View File

@@ -39,9 +39,7 @@ class VariantsStockLevels
def scoped_variant(distributor, variant)
return variant if distributor.blank?
scoper(distributor).scope(
variant, inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, distributor)
)
scoper(distributor).scope(variant)
variant
end

View File

@@ -15,11 +15,7 @@ module OrderManagement
variant = line_item.variant
next unless variant.stock_item
OpenFoodNetwork::ScopeVariantToHub.new(order.distributor).scope(
variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory,
order.distributor)
)
OpenFoodNetwork::ScopeVariantToHub.new(order.distributor).scope(variant)
on_hand, backordered = variant.fill_status(line_item.quantity)
package.add variant, on_hand, :on_hand if on_hand.positive?

View File

@@ -34,9 +34,7 @@ module OrderManagement
def price_estimate_for(variant, fallback)
return fallback unless fee_calculator && variant
scoper.scope(
variant, inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory, shop)
)
scoper.scope(variant)
fees = fee_calculator.indexed_fees_for(variant)
(variant.price + fees).to_d

View File

@@ -7,8 +7,8 @@ module OpenFoodNetwork
@variant_overrides = variant_overrides || VariantOverride.indexed(@hub)
end
def scope(variant, inventory_enabled: true)
return unless inventory_enabled
def scope(variant)
return unless OpenFoodNetwork::FeatureToggle.enabled?(:inventory, @hub)
variant.extend(OpenFoodNetwork::ScopeVariantToHub::ScopeVariantToHub)
variant.instance_variable_set :@hub, @hub

View File

@@ -20,11 +20,7 @@ module Spree
if order&.line_items.present?
scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor)
order.line_items.each do |li|
scoper.scope(
li.variant,
inventory_enabled: OpenFoodNetwork::FeatureToggle.enabled?(:inventory,
order.distributor)
)
scoper.scope(li.variant)
end
end

View File

@@ -22,7 +22,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
}
let(:scoper) { described_class.new(hub) }
describe "overriding price" do
describe "overriding price", feature: :inventory do
it "returns the overridden price when one is present" do
vo
scoper.scope v
@@ -35,7 +35,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
end
end
describe "overriding price_in" do
describe "overriding price_in", feature: :inventory do
it "returns the overridden price when one is present" do
vo
scoper.scope v
@@ -48,7 +48,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
end
end
describe "overriding stock levels" do
describe "overriding stock levels", feature: :inventory do
it "returns the overridden stock level when one is present" do
vo
scoper.scope v
@@ -60,7 +60,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
expect(v.on_hand).to eq(1)
end
describe "overriding stock on an on_demand variant" do
describe "overriding stock on an on_demand variant", feature: :inventory do
let(:v) { create(:variant, price: 11.11, on_demand: true) }
it "clears on_demand when the stock is overridden" do
@@ -81,7 +81,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
end
end
describe "overriding on_demand" do
describe "overriding on_demand", feature: :inventory do
context "when an override exists" do
before { vo }
@@ -124,7 +124,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
# in_stock? is indirectly overridden through can_supply?
# can_supply? is indirectly overridden by on_demand and total_on_hand
# these tests validate this chain is working correctly
describe "overriding in_stock?" do
describe "overriding in_stock?", feature: :inventory do
before { v.on_demand = false }
context "when an override exists" do
@@ -173,7 +173,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
end
end
describe "overriding #move" do
describe "overriding #move", feature: :inventory do
context "when override is on_demand" do
before do
vo2
@@ -205,7 +205,7 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
end
end
describe "overriding sku" do
describe "overriding sku", feature: :inventory do
context "when an override exists" do
before { vo }
@@ -233,13 +233,13 @@ RSpec.describe OpenFoodNetwork::ScopeVariantToHub do
end
end
end
end
context "with inventory is disabled" do
it "doesn't override the variant" do
vo
scoper.scope(v, inventory_enabled: false)
expect(v.price).to eq(11.11)
end
context "with inventory is disabled" do
it "doesn't override the variant" do
vo
scoper.scope(v)
expect(v.price).to eq(11.11)
end
end
end

View File

@@ -343,7 +343,7 @@ RSpec.describe Spree::LineItem do
let!(:line_item) { order.reload.line_items.first }
let!(:variant) { line_item.variant }
context "when a variant override applies" do
context "when a variant override applies", feature: :inventory do
let!(:vo) { create(:variant_override, hub: shop, variant:, count_on_hand: 3 ) }
it "draws stock from the variant override" do