diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index 51bb1468f7..a552cb98bb 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -25,12 +25,21 @@ class VariantOverride < ActiveRecord::Base if vo.nil? Bugsnag.notify RuntimeError.new "Attempting to decrement stock level for a variant without a VariantOverride." - - elsif vo.count_on_hand.blank? - Bugsnag.notify RuntimeError.new "Attempting to decrement stock level on a VariantOverride without a count_on_hand specified." - else - vo.decrement! :count_on_hand, quantity + vo.decrement_stock! quantity + end + end + + + def stock_overridden? + count_on_hand.present? + end + + def decrement_stock!(quantity) + if stock_overridden? + decrement! :count_on_hand, quantity + else + Bugsnag.notify RuntimeError.new "Attempting to decrement stock level on a VariantOverride without a count_on_hand specified." end end diff --git a/lib/open_food_network/scope_variant_to_hub.rb b/lib/open_food_network/scope_variant_to_hub.rb index 4e6e454368..d21f3ede1f 100644 --- a/lib/open_food_network/scope_variant_to_hub.rb +++ b/lib/open_food_network/scope_variant_to_hub.rb @@ -7,12 +7,13 @@ module OpenFoodNetwork def scope(variant) variant.send :extend, OpenFoodNetwork::ScopeVariantToHub::ScopeVariantToHub variant.instance_variable_set :@hub, @hub + variant.instance_variable_set :@variant_override, VariantOverride.send(:for, @hub, variant) end module ScopeVariantToHub def price - VariantOverride.price_for(@hub, self) || super + @variant_override.andand.price || super end def price_in(currency) @@ -20,12 +21,12 @@ module OpenFoodNetwork end def count_on_hand - VariantOverride.count_on_hand_for(@hub, self) || super + @variant_override.andand.count_on_hand || super end def decrement!(attribute, by=1) - if attribute == :count_on_hand && VariantOverride.stock_overridden?(@hub, self) - VariantOverride.decrement_stock! @hub, self, by + if attribute == :count_on_hand && @variant_override.andand.stock_overridden? + @variant_override.decrement_stock! by else super end diff --git a/spec/models/spree/order_populator_spec.rb b/spec/models/spree/order_populator_spec.rb index c4ee8e44b6..ef72106e16 100644 --- a/spec/models/spree/order_populator_spec.rb +++ b/spec/models/spree/order_populator_spec.rb @@ -50,6 +50,7 @@ module Spree variant = double(:variant) quantity = 123 Spree::Variant.stub(:find).and_return(variant) + VariantOverride.stub(:for).and_return(nil) op.should_receive(:check_stock_levels).with(variant, quantity).and_return(true) op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true)