From 75a79717cf0edbeb64145227996273b549431364 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 15 Feb 2019 12:41:52 +0000 Subject: [PATCH 1/4] Adapt scope_variant_to_hub to new VO rules where on_demand nil is seen as use_producer_settings --- app/models/variant_override.rb | 2 +- lib/open_food_network/scope_variant_to_hub.rb | 18 ++++++------------ .../scope_variant_to_hub_spec.rb | 5 +++-- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index dd3b22a5e0..cea630719b 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -57,7 +57,7 @@ class VariantOverride < ActiveRecord::Base end def stock_overridden? - count_on_hand.present? + on_demand == false && count_on_hand.present? end def decrement_stock!(quantity) diff --git a/lib/open_food_network/scope_variant_to_hub.rb b/lib/open_food_network/scope_variant_to_hub.rb index bc2afa63da..55d8dd92b1 100644 --- a/lib/open_food_network/scope_variant_to_hub.rb +++ b/lib/open_food_network/scope_variant_to_hub.rb @@ -29,21 +29,15 @@ module OpenFoodNetwork end def count_on_hand - @variant_override.andand.count_on_hand || super + return super unless @variant_override.andand.stock_overridden? + + @variant_override.count_on_hand end def on_demand - if @variant_override.andand.on_demand.nil? - if @variant_override.andand.count_on_hand.present? - # If we're overriding the stock level of an on_demand variant, show it as not - # on_demand, so our stock control can take effect. - false - else - super - end - else - @variant_override.andand.on_demand - end + return super if @variant_override.andand.on_demand.nil? + + @variant_override.andand.on_demand 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 aa5ce4b317..03f19c62eb 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 @@ -1,3 +1,4 @@ +require 'spec_helper' require 'open_food_network/scope_variant_to_hub' module OpenFoodNetwork @@ -82,9 +83,9 @@ module OpenFoodNetwork before { vo.update_column(:on_demand, nil) } context "when count_on_hand is set" do - it "returns false" do + it "returns variant's on_demand" do scoper.scope v - expect(v.on_demand).to be false + expect(v.on_demand).to be true end end From ef786adcfce6bb37adf6777bf1bb3e4111413105 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 20 Feb 2019 13:07:21 +0000 Subject: [PATCH 2/4] Make variant_override.stock_overridden? simpler and add comment to explain relationship between count_on_hand and on_demand in a VO --- app/models/variant_override.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index cea630719b..a00caef367 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -57,7 +57,9 @@ class VariantOverride < ActiveRecord::Base end def stock_overridden? - on_demand == false && count_on_hand.present? + # If count_on_hand is present, it means on_demand is false + # See StockSettingsOverrideValidation for details + count_on_hand.present? end def decrement_stock!(quantity) From 45e5fed609b11776f162eba9382ab5f65a4bc411 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 21 Feb 2019 11:25:01 +0000 Subject: [PATCH 3/4] Improve scope_variant_to_hub code and spec by testing an invalid data state --- app/models/variant_override.rb | 4 ++++ lib/open_food_network/scope_variant_to_hub.rb | 16 ++++++++++------ .../scope_variant_to_hub_spec.rb | 14 +++++++------- 3 files changed, 21 insertions(+), 13 deletions(-) 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 From 0b1ea1beda14016ddca2d97ad07336989044c819 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Fri, 22 Feb 2019 09:41:25 +0000 Subject: [PATCH 4/4] Rename VariantOverride.use_producer_settings to VariantOverride.use_producer_stock_settings --- app/models/variant_override.rb | 2 +- lib/open_food_network/scope_variant_to_hub.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/variant_override.rb b/app/models/variant_override.rb index 6b43822e6c..355a46d1db 100644 --- a/app/models/variant_override.rb +++ b/app/models/variant_override.rb @@ -62,7 +62,7 @@ class VariantOverride < ActiveRecord::Base count_on_hand.present? end - def use_producer_settings? + def use_producer_stock_settings? on_demand.nil? end diff --git a/lib/open_food_network/scope_variant_to_hub.rb b/lib/open_food_network/scope_variant_to_hub.rb index d838e253cb..a721505f7f 100644 --- a/lib/open_food_network/scope_variant_to_hub.rb +++ b/lib/open_food_network/scope_variant_to_hub.rb @@ -37,7 +37,7 @@ module OpenFoodNetwork end def on_demand - if @variant_override.present? && !@variant_override.use_producer_settings? + if @variant_override.present? && !@variant_override.use_producer_stock_settings? @variant_override.on_demand else super