From a18d289292f45c4201799bfa21f41acd5c419445 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 29 Nov 2018 13:19:30 +0000 Subject: [PATCH 1/2] Scope variants on line_item before save hook update_inventory --- app/models/spree/line_item_decorator.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 516c4e70c2..7bc718e857 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -127,6 +127,12 @@ Spree::LineItem.class_eval do private + def update_inventory_with_scoping + scoper.scope(variant) + update_inventory_without_scoping + end + alias_method_chain :update_inventory, :scoping + # Override of Spree validation method # Added check for in-memory :skip_stock_check attribute def stock_availability @@ -135,8 +141,7 @@ Spree::LineItem.class_eval do end def scoper - return @scoper unless @scoper.nil? - @scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) + @scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) end def calculate_final_weight_volume From 782530d93b2a24b4b7aabf70dcd8c17d763c9cae Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 28 Nov 2018 11:37:23 +0000 Subject: [PATCH 2/2] Scope variants in shipment manifest as they are fetched from DB --- app/models/spree/shipment_decorator.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/models/spree/shipment_decorator.rb b/app/models/spree/shipment_decorator.rb index e9cdc1ed31..735fdb64cd 100644 --- a/app/models/spree/shipment_decorator.rb +++ b/app/models/spree/shipment_decorator.rb @@ -15,6 +15,18 @@ module Spree end end + # The shipment manifest is built by loading inventory units and variants from the DB + # These variants come unscoped + # So, we need to scope the variants just after the manifest is built + def manifest_with_scoping + manifest_without_scoping.each { |item| scoper.scope(item.variant) } + end + alias_method_chain :manifest, :scoping + + def scoper + @scoper ||= OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) + end + private # NOTE: This is an override of spree's method, needed to allow orders