diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index fae1ff9dde..494172aedf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -27,6 +27,14 @@ class ApplicationController < ActionController::Base @total_distributors = Enterprise.is_distributor.distinct_count end + def require_distributor_chosen + unless current_order(false).andand.distributor + redirect_to spree.root_path + false + 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 diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index b59afaa07e..3d376242ed 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -1,7 +1,8 @@ -include Spree::ProductsHelper -include OrderCyclesHelper - class EnterprisesController < BaseController + include Spree::ProductsHelper + include OrderCyclesHelper + + before_filter :require_distributor_chosen, only: :show def index @enterprises = Enterprise.all diff --git a/app/controllers/spree/products_controller_decorator.rb b/app/controllers/spree/products_controller_decorator.rb index 2f6468c0b5..9d89de2aca 100644 --- a/app/controllers/spree/products_controller_decorator.rb +++ b/app/controllers/spree/products_controller_decorator.rb @@ -5,6 +5,8 @@ Spree::ProductsController.class_eval do include OrderCyclesHelper include OpenFoodWeb::SplitProductsByDistribution + before_filter :require_distributor_chosen, only: [:index, :show] + respond_override :index => { :html => { :success => lambda { if current_order_cycle order_cycle_products = current_order_cycle.products diff --git a/app/controllers/spree/taxons_controller_decorator.rb b/app/controllers/spree/taxons_controller_decorator.rb index 7077f2b604..fc7a22915a 100644 --- a/app/controllers/spree/taxons_controller_decorator.rb +++ b/app/controllers/spree/taxons_controller_decorator.rb @@ -5,6 +5,8 @@ Spree::TaxonsController.class_eval do include OrderCyclesHelper include OpenFoodWeb::SplitProductsByDistribution + before_filter :require_distributor_chosen, only: :show + respond_override :show => { :html => { :success => lambda { @products, @products_local, @products_remote = split_products_by_distribution @products, current_distributor, current_order_cycle } } } diff --git a/spec/controllers/enterprises_controller_spec.rb b/spec/controllers/enterprises_controller_spec.rb index 8f35633651..81d347cd3f 100644 --- a/spec/controllers/enterprises_controller_spec.rb +++ b/spec/controllers/enterprises_controller_spec.rb @@ -10,7 +10,7 @@ describe EnterprisesController do assigns(:suppliers).should == [s] end - context 'shopping for a distributor' do + context "shopping for a distributor" do before(:each) do @current_distributor = create(:distributor_enterprise) @@ -60,4 +60,12 @@ describe EnterprisesController do controller.current_order.order_cycle.should == @order_cycle1 end end + + context "when a distributor has not been chosen" do + it "redirects #show to distributor selection" do + @distributor = create(:distributor_enterprise) + spree_get :show, {id: @distributor} + response.should redirect_to spree.root_path + end + end end diff --git a/spec/controllers/spree/products_controller_spec.rb b/spec/controllers/spree/products_controller_spec.rb new file mode 100644 index 0000000000..95ce81558d --- /dev/null +++ b/spec/controllers/spree/products_controller_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Spree::ProductsController do + context "when a distributor has not been chosen" do + it "redirects #index to distributor selection" do + spree_get :index + response.should redirect_to spree.root_path + end + + it "redirects #show to distributor selection" do + product = create(:simple_product) + spree_get :show, {id: product.permalink} + response.should redirect_to spree.root_path + end + end +end diff --git a/spec/controllers/spree/taxons_controller_spec.rb b/spec/controllers/spree/taxons_controller_spec.rb new file mode 100644 index 0000000000..9ef4a34cc1 --- /dev/null +++ b/spec/controllers/spree/taxons_controller_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Spree::TaxonsController do + context "when a distributor has not been chosen" do + it "redirects #show to distributor selection" do + taxon = create(:taxon) + spree_get :show, {id: taxon.permalink} + response.should redirect_to spree.root_path + end + end +end