diff --git a/lib/open_food_network/scope_variants_for_search.rb b/lib/open_food_network/scope_variants_for_search.rb index 2bfa88a804..1ffc03bcd3 100644 --- a/lib/open_food_network/scope_variants_for_search.rb +++ b/lib/open_food_network/scope_variants_for_search.rb @@ -16,7 +16,7 @@ module OpenFoodNetwork def search @variants = query_scope - scope_to_in_stock_only if params[:distributor_id] && params[:include_out_of_stock] != "1" + scope_to_in_stock_only if scope_to_in_stock_only? scope_to_schedule if params[:schedule_id] scope_to_order_cycle if params[:order_cycle_id] scope_to_distributor if params[:distributor_id] @@ -72,7 +72,8 @@ module OpenFoodNetwork def scope_to_in_stock_only @variants = @variants.joins( "INNER JOIN spree_stock_items ON spree_stock_items.variant_id = spree_variants.id - LEFT JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id" + LEFT JOIN variant_overrides ON variant_overrides.variant_id = spree_variants.id AND + variant_overrides.hub_id = #{distributor.id}" ).where(" variant_overrides.on_demand IS TRUE OR variant_overrides.count_on_hand > 0 OR @@ -81,6 +82,10 @@ module OpenFoodNetwork ") end + def scope_to_in_stock_only? + params[:distributor_id] && params[:include_out_of_stock] != "1" + end + def scope_variants_to_distributor(variants, distributor) scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor) # Perform scoping after all filtering is done. diff --git a/spec/lib/open_food_network/scope_variants_to_search_spec.rb b/spec/lib/open_food_network/scope_variants_to_search_spec.rb index 39ad17bd46..c317aa9196 100644 --- a/spec/lib/open_food_network/scope_variants_to_search_spec.rb +++ b/spec/lib/open_food_network/scope_variants_to_search_spec.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'spec_helper' require 'open_food_network/scope_variants_for_search' describe OpenFoodNetwork::ScopeVariantsForSearch do @@ -85,7 +86,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do create(:variant_override, variant: variant, hub: d1, on_demand: false, count_on_hand: 0) variant end - let!(:distributor1_variant_with_override_without_stock_level_set_and_producer_not_in_stock) do + let!(:distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock) do variant = create(:simple_product).variants.first variant.stock_items.first.update!(backorderable: false, count_on_hand: 0) create(:simple_order_cycle, distributors: [d1], variants: [variant]) @@ -100,7 +101,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do variant end let!(:distributor2_variant_with_override_in_stock) do - create_variant_with_variant_override_for(d2, count_on_hand: 1) + create_variant_with_variant_override_for(d2, on_demand: true, count_on_hand: nil) end context "when :include_out_of_stock is not specified" do @@ -120,7 +121,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do distributor1_variant_not_backorderable_and_not_on_hand, distributor1_variant_with_override_not_on_demand_and_not_on_hand, distributor1_variant_with_override_not_in_stock_but_producer_in_stock, - distributor1_variant_with_override_without_stock_level_set_and_producer_not_in_stock, + distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock, distributor2_variant_with_override_in_stock ) end @@ -136,7 +137,7 @@ describe OpenFoodNetwork::ScopeVariantsForSearch do distributor1_variant_with_override_on_demand_but_not_on_hand, distributor1_variant_with_override_on_hand_but_not_on_demand, distributor1_variant_with_override_without_stock_level_set_but_producer_in_stock, - distributor1_variant_with_override_without_stock_level_set_and_producer_not_in_stock, + distributor1_variant_with_override_without_stock_level_set_and_no_producer_stock, distributor1_variant_not_backorderable_and_not_on_hand, distributor1_variant_with_override_not_on_demand_and_not_on_hand, distributor1_variant_with_override_not_in_stock_but_producer_in_stock