Move #check_order_cycle_expiry method to OrderStockCheck and don't call it from BaseController :before_action callback

This commit is contained in:
Matt-Yorkley
2022-01-29 12:10:55 +00:00
parent 6dac65ace5
commit 5e6dd1e6e1
5 changed files with 25 additions and 25 deletions

View File

@@ -139,17 +139,6 @@ class ApplicationController < ActionController::Base
!current_distributor.ready_for_checkout?
end
def check_order_cycle_expiry
if current_order_cycle&.closed?
Bugsnag.notify("Notice: order cycle closed during checkout completion", order: current_order)
current_order.empty!
current_order.set_order_cycle! nil
flash[:info] = I18n.t('order_cycle_closed')
redirect_to main_app.shop_path
end
end
# All render calls within the block will be performed with the specified format
# Useful for rendering html within a JSON response, particularly if the specified
# template or partial then goes on to render further partials without specifying

View File

@@ -12,7 +12,6 @@ class BaseController < ApplicationController
include OrderCyclesHelper
before_action :set_locale
before_action :check_order_cycle_expiry
private

View File

@@ -18,6 +18,17 @@ module OrderStockCheck
redirect_to main_app.cart_path
end
def check_order_cycle_expiry
if current_order_cycle&.closed?
Bugsnag.notify("Notice: order cycle closed during checkout completion", order: current_order)
current_order.empty!
current_order.set_order_cycle! nil
flash[:info] = I18n.t('order_cycle_closed')
redirect_to main_app.shop_path
end
end
private
def sufficient_stock?

View File

@@ -99,17 +99,4 @@ describe BaseController, type: :controller do
controller.current_order(true)
end
end
it "redirects to shopfront with message if order cycle is expired" do
expect(controller).to receive(:current_order_cycle).and_return(oc)
expect(controller).to receive(:current_order).and_return(order).at_least(:twice)
expect(oc).to receive(:closed?).and_return(true)
expect(order).to receive(:empty!)
expect(order).to receive(:set_order_cycle!).with(nil)
get :index
expect(response).to redirect_to shop_url
expect(flash[:info]).to eq I18n.t('order_cycle_closed')
end
end

View File

@@ -25,6 +25,20 @@ describe CheckoutController, type: :controller do
expect(response).to redirect_to shop_path
end
it "redirects to shopfront with message if order cycle is expired" do
allow(controller).to receive(:current_distributor).and_return(distributor)
expect(controller).to receive(:current_order_cycle).and_return(order_cycle).at_least(:once)
expect(controller).to receive(:current_order).and_return(order).at_least(:once)
expect(order_cycle).to receive(:closed?).and_return(true)
expect(order).to receive(:empty!)
expect(order).to receive(:set_order_cycle!).with(nil)
get :edit
expect(response).to redirect_to shop_url
expect(flash[:info]).to eq I18n.t('order_cycle_closed')
end
it "redirects home with message if hub is not ready for checkout" do
allow(distributor).to receive(:ready_for_checkout?) { false }
allow(order).to receive_messages(distributor: distributor, order_cycle: order_cycle)