diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bc4993b790..d95e6d55b2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -35,6 +35,15 @@ class ApplicationController < ActionController::Base end end + def check_order_cycle_expiry + if current_order_cycle.andand.expired? + session[:expired_order_cycle_id] = current_order_cycle.id + current_order.empty! + current_order.set_order_cycle! nil + redirect_to spree.order_cycle_expired_orders_path + end + end + # There are several domains that point to the production server, but only one # (vic.openfoodnetwork.org) that has the SSL certificate. Redirect all requests to this # domain to avoid showing customers a scary invalid certificate error. diff --git a/app/controllers/base_controller.rb b/app/controllers/base_controller.rb index 5617a35918..dcf4797448 100644 --- a/app/controllers/base_controller.rb +++ b/app/controllers/base_controller.rb @@ -7,4 +7,6 @@ class BaseController < ApplicationController # Spree::Core::ControllerHelpers declares helper_method get_taxonomies, so we need to # include Spree::ProductsHelper so that method is available on the controller include Spree::ProductsHelper + + before_filter :check_order_cycle_expiry end diff --git a/app/controllers/spree/store_controller_decorator.rb b/app/controllers/spree/store_controller_decorator.rb index d25f1cad6f..00c28176cb 100644 --- a/app/controllers/spree/store_controller_decorator.rb +++ b/app/controllers/spree/store_controller_decorator.rb @@ -1,18 +1,4 @@ Spree::StoreController.class_eval do include OrderCyclesHelper - before_filter :check_order_cycle_expiry - - - private - - def check_order_cycle_expiry - if current_order_cycle.andand.expired? - session[:expired_order_cycle_id] = current_order_cycle.id - current_order.empty! - current_order.set_order_cycle! nil - redirect_to order_cycle_expired_orders_path - end - end - end diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index 81d347cd3f..43d965138c 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -68,4 +68,20 @@ describe EnterprisesController do response.should redirect_to spree.root_path end end + + context "BaseController: handling order cycles expiring mid-order" do + it "clears the order and displays an expiry message" do + oc = double(:order_cycle, id: 123, expired?: true) + controller.stub(:current_order_cycle) { oc } + + order = double(:order) + order.should_receive(:empty!) + order.should_receive(:set_order_cycle!).with(nil) + controller.stub(:current_order) { order } + + spree_get :index + session[:expired_order_cycle_id].should == 123 + response.should redirect_to spree.order_cycle_expired_orders_path + end + end end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 319af7ba78..0451e35431 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -61,4 +61,20 @@ describe Spree::HomeController do spree_get :index end end + + context "StoreController: handling order cycles expiring mid-order" do + it "clears the order and displays an expiry message" do + oc = double(:order_cycle, id: 123, expired?: true) + controller.stub(:current_order_cycle) { oc } + + order = double(:order) + order.should_receive(:empty!) + order.should_receive(:set_order_cycle!).with(nil) + controller.stub(:current_order) { order } + + spree_get :index + session[:expired_order_cycle_id].should == 123 + response.should redirect_to spree.order_cycle_expired_orders_path + end + end end