Redirect if distributor not chosen

This commit is contained in:
Rohan Mitchell
2013-09-23 13:39:13 +10:00
parent 000b13782e
commit 072ce67b78
7 changed files with 52 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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
} } }

View File

@@ -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

View File

@@ -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

View File

@@ -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