mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
When user has selected a hub that is not ready for checkout, unselect it
This commit is contained in:
@@ -32,6 +32,15 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
def check_hub_ready_for_checkout
|
||||
if current_distributor && !current_distributor.ready_for_checkout?
|
||||
current_order.empty!
|
||||
current_order.set_distribution! nil, nil
|
||||
flash[:info] = "The hub you have selected is temporarily closed for orders. Please try again later."
|
||||
redirect_to root_url
|
||||
end
|
||||
end
|
||||
|
||||
def check_order_cycle_expiry
|
||||
if current_order_cycle.andand.closed?
|
||||
session[:expired_order_cycle_id] = current_order_cycle.id
|
||||
|
||||
@@ -10,6 +10,7 @@ class BaseController < ApplicationController
|
||||
# include Spree::ProductsHelper so that method is available on the controller
|
||||
include Spree::ProductsHelper
|
||||
|
||||
before_filter :check_hub_ready_for_checkout
|
||||
before_filter :check_order_cycle_expiry
|
||||
|
||||
def load_active_distributors
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
class CheckoutController < Spree::CheckoutController
|
||||
layout 'darkswarm'
|
||||
|
||||
prepend_before_filter :check_hub_ready_for_checkout
|
||||
prepend_before_filter :check_order_cycle_expiry
|
||||
prepend_before_filter :require_order_cycle
|
||||
prepend_before_filter :require_distributor_chosen
|
||||
|
||||
@@ -1,26 +1,44 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe BaseController do
|
||||
let(:oc) { mock_model(OrderCycle) }
|
||||
let(:order) { mock_model(Spree::Order) }
|
||||
let(:oc) { mock_model(OrderCycle) }
|
||||
let(:hub) { mock_model(Enterprise, ready_for_checkout?: true) }
|
||||
let(:order) { mock_model(Spree::Order, distributor: hub) }
|
||||
controller(BaseController) do
|
||||
def index
|
||||
render text: ""
|
||||
end
|
||||
end
|
||||
|
||||
it "redirects to home with message if order cycle is expired" do
|
||||
controller.stub(:current_order_cycle).and_return oc
|
||||
controller.stub(:current_order).and_return order
|
||||
order.stub(:empty!)
|
||||
order.stub(:set_order_cycle!)
|
||||
oc.stub(:closed?).and_return true
|
||||
controller.stub(:current_order_cycle).and_return(oc)
|
||||
controller.stub(:current_order).and_return(order)
|
||||
oc.stub(:closed?).and_return(true)
|
||||
|
||||
order.should_receive(:empty!)
|
||||
order.should_receive(:set_order_cycle!).with(nil)
|
||||
|
||||
get :index
|
||||
|
||||
response.should redirect_to root_url
|
||||
flash[:info].should == "The order cycle you've selected has just closed. Please try again!"
|
||||
end
|
||||
|
||||
it "redirects to home with message if hub is not ready for checkout" do
|
||||
hub.stub(:ready_for_checkout?) { false }
|
||||
controller.stub(:current_order).and_return(order)
|
||||
|
||||
order.should_receive(:empty!)
|
||||
order.should_receive(:set_distribution!).with(nil, nil)
|
||||
|
||||
get :index
|
||||
|
||||
response.should redirect_to root_url
|
||||
flash[:info].should == "The hub you have selected is temporarily closed for orders. Please try again later."
|
||||
end
|
||||
|
||||
it "loads active_distributors" do
|
||||
Enterprise.should_receive(:distributors_with_active_order_cycles)
|
||||
controller.load_active_distributors
|
||||
Enterprise.stub_chain(:distributors_with_active_order_cycles, :ready_for_checkout) { 'active distributors' }
|
||||
controller.load_active_distributors.should == 'active distributors'
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user