mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Extract Shop::OrderCyclesList from base_controller
This commit is contained in:
@@ -29,23 +29,11 @@ class BaseController < ApplicationController
|
||||
return
|
||||
end
|
||||
|
||||
fetch_order_cycles(@distributor)
|
||||
@order_cycles = Shop::OrderCyclesList.new(@distributor, current_customer).call
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
# Default to the only order cycle if there's only one
|
||||
#
|
||||
# Here we need to use @order_cycles.size not @order_cycles.count
|
||||
|
||||
@@ -96,8 +96,8 @@ class EnterprisesController < BaseController
|
||||
end
|
||||
|
||||
def reset_order_cycle(order, distributor)
|
||||
fetch_order_cycles(distributor)
|
||||
order.order_cycle = @order_cycles.first if @order_cycles.size == 1
|
||||
order_cycles = Shop::OrderCyclesList.new(distributor, current_customer).call
|
||||
order.order_cycle = order_cycles.first if order_cycles.size == 1
|
||||
end
|
||||
|
||||
def shop_order_cycles
|
||||
|
||||
22
app/services/shop/order_cycles_list.rb
Normal file
22
app/services/shop/order_cycles_list.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Lists available order cycles for a given customer in a given distributor
|
||||
|
||||
class OrderCyclesList
|
||||
def initialize(distributor, customer)
|
||||
@distributor = distributor
|
||||
@customer = customer
|
||||
end
|
||||
|
||||
def call
|
||||
order_cycles = OrderCycle.with_distributor(@distributor).active
|
||||
.order(@distributor.preferred_shopfront_order_cycle_order)
|
||||
|
||||
applicator = OpenFoodNetwork::TagRuleApplicator.new(@distributor,
|
||||
"FilterOrderCycles",
|
||||
@customer.andand.tag_list)
|
||||
applicator.filter!(order_cycles)
|
||||
|
||||
order_cycles
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user