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
This commit is contained in:
Luis Ramos
2020-02-20 19:19:04 +00:00
parent d2eee1dafd
commit d5cf355a11
2 changed files with 14 additions and 8 deletions

View File

@@ -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

View File

@@ -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