diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index a00caef367..6b43822e6c 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -62,6 +62,10 @@ class VariantOverride < ActiveRecord::Base count_on_hand.present? end + def use_producer_settings? + on_demand.nil? + end + def decrement_stock!(quantity) if stock_overridden? decrement! :count_on_hand, quantity diff --git a/lib/open_food_network/scope_variant_to_hub.rb b/lib/open_food_network/scope_variant_to_hub.rb index 55d8dd92b1..d838e253cb 100644 --- a/lib/open_food_network/scope_variant_to_hub.rb +++ b/lib/open_food_network/scope_variant_to_hub.rb @@ -29,15 +29,19 @@ module OpenFoodNetwork end def count_on_hand - return super unless @variant_override.andand.stock_overridden? - - @variant_override.count_on_hand + if @variant_override.present? && @variant_override.stock_overridden? + @variant_override.count_on_hand + else + super + end end def on_demand - return super if @variant_override.andand.on_demand.nil? - - @variant_override.andand.on_demand + if @variant_override.present? && !@variant_override.use_producer_settings? + @variant_override.on_demand + else + super + end end def decrement!(attribute, by = 1) diff --git a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb index 03f19c62eb..4f0ec5f13e 100644 --- a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb +++ b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb @@ -82,13 +82,6 @@ module OpenFoodNetwork context "without an on_demand set" do before { vo.update_column(:on_demand, nil) } - context "when count_on_hand is set" do - it "returns variant's on_demand" do - scoper.scope v - expect(v.on_demand).to be true - end - end - context "when count_on_hand is not set" do before { vo.update_column(:count_on_hand, nil) } @@ -97,6 +90,13 @@ module OpenFoodNetwork expect(v.on_demand).to be true end end + + context "when count_on_hand is set" do + it "should return validation error on save" do + scoper.scope v + expect{ vo.save! }.to raise_error ActiveRecord::RecordInvalid + end + end end end