From d5cf355a11b57553ad300e5cf44391d884fcabc5 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 20 Feb 2020 19:19:04 +0000 Subject: [PATCH] Call fetch_order_cycles from reset_order_cycles so we dont repeat the calculation BaseController#set_order_cycle cant be used in reset_order_cycle because it will empty the order if the OC is not defined previously --- app/controllers/base_controller.rb | 18 ++++++++++++------ app/controllers/enterprises_controller.rb | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index 45b6a6f155..321a4c7e35 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -29,15 +29,21 @@ class BaseController < ApplicationController return end - @order_cycles = OrderCycle.with_distributor(@distributor).active - .order(@distributor.preferred_shopfront_order_cycle_order) + fetch_order_cycles(@distributor) - applicator = OpenFoodNetwork::TagRuleApplicator.new(@distributor, + set_order_cycle + end + + def fetch_order_cycles(distributor) + return if @order_cycles.present? + + @order_cycles = OrderCycle.with_distributor(distributor).active + .order(distributor.preferred_shopfront_order_cycle_order) + + applicator = OpenFoodNetwork::TagRuleApplicator.new(distributor, "FilterOrderCycles", current_customer.andand.tag_list) applicator.filter!(@order_cycles) - - reset_order_cycle end # Default to the only order cycle if there's only one @@ -45,7 +51,7 @@ class BaseController < ApplicationController # Here we need to use @order_cycles.size not @order_cycles.count # because TagRuleApplicator changes ActiveRecord::Relation @order_cycles # and these changes are not seen if the relation is reloaded with count - def reset_order_cycle + def set_order_cycle return if @order_cycles.size != 1 current_order(true).set_order_cycle! @order_cycles.first diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index c1b5b5104f..6e3e0f0c0a 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -96,8 +96,8 @@ class EnterprisesController < BaseController end def reset_order_cycle(order, distributor) - order_cycle_options = OrderCycle.active.with_distributor(distributor) - order.order_cycle = order_cycle_options.first if order_cycle_options.count == 1 + fetch_order_cycles(distributor) + order.order_cycle = @order_cycles.first if @order_cycles.size == 1 end def shop_order_cycles