Files
openfoodnetwork/app/controllers/base_controller.rb
Cillian O'Ruanaidh a46b77d10c Ensure hack for showing products on a shop with closed order cycles also works if an order cycle has no shipping methods
If a distributor order cycle has no shipping methods it will be invalid. This is a bit confusing because adding a check for :invalid seems like it would be better if it was done in the Shop::OrderCyclesList service but the hack doesn't work if you put it in there, it seems like something needs a refactor.
2022-09-30 13:13:39 +01:00

44 lines
1.1 KiB
Ruby

# frozen_string_literal: true
require 'spree/core/controller_helpers/order'
require 'open_food_network/tag_rule_applicator'
class BaseController < ApplicationController
layout 'darkswarm'
include Spree::Core::ControllerHelpers::Order
include I18nHelper
include OrderCyclesHelper
before_action :set_locale
private
def all_distributor_order_cycles_invalid?
OrderCycle.with_distributor(@distributor).active.all?(&:invalid?)
end
def set_order_cycles
if !@distributor.ready_for_checkout? || all_distributor_order_cycles_invalid?
@order_cycles = OrderCycle.where('false')
return
end
@order_cycles = Shop::OrderCyclesList.new(@distributor, current_customer).call
set_order_cycle
end
# Default to the only order cycle if there's only one
#
# Here we need to use @order_cycles.size not @order_cycles.count
# because OrderCyclesList returns a modified ActiveRecord::Relation
# and these modifications are not seen if it is reloaded with count
def set_order_cycle
return if @order_cycles.size != 1
current_order(true).set_order_cycle! @order_cycles.first
end
end