From 8488b8e4698347f77f6480fff53c0c9929116d8d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 31 Oct 2014 15:02:58 +1100 Subject: [PATCH] When user has selected a hub that is not ready for checkout, unselect it --- app/controllers/application_controller.rb | 9 ++++++ app/controllers/base_controller.rb | 1 + app/controllers/checkout_controller.rb | 1 + spec/controllers/base_controller_spec.rb | 36 +++++++++++++++++------ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index decc48ac7e..3b76ee3168 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index 88c0f89aec..fbb5d44784 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -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 diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index 7e8b04afda..024c250ede 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -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 diff --git a/spec/controllers/base_controller_spec.rb b/spec/controllers/base_controller_spec.rb index e431f14e3f..4337107615 100644 --- a/spec/controllers/base_controller_spec.rb +++ b/spec/controllers/base_controller_spec.rb @@ -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