From 71a5d84a1da0817b3fe058d797b1a03dc96001bc Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 11 Dec 2013 15:06:07 +1100 Subject: [PATCH] Filtering the products to the current order cycle --- app/controllers/enterprises_controller.rb | 1 - app/controllers/shop_controller.rb | 4 ++-- spec/controllers/shop_controller_spec.rb | 22 +++++++++++----------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index 383c52df07..d4b6ade04b 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -65,7 +65,6 @@ class EnterprisesController < BaseController order_cycle_options = OrderCycle.active.with_distributor(distributor) order.order_cycle = order_cycle_options.first if order_cycle_options.count == 1 - order.save! redirect_to main_app.enterprise_path(distributor) diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index dca51d71b5..880e66e8e0 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -8,8 +8,8 @@ class ShopController < BaseController end def products - if current_order_cycle - render json: Spree::Product.all.to_json + if products = current_order_cycle.andand.products_distributed_by(@distributor) + render json: products.to_json else render json: "", status: 404 end diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 6f57add80c..0c8575231c 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -13,13 +13,6 @@ describe ShopController do controller.stub(:current_distributor).and_return d end - describe "Fetching products" do - it "should return products for the current order cycle" do - spree_get :products - end - it "should not return other products" - end - describe "Selecting order cycles" do it "should select an order cycle when only one order cycle is open" do oc1 = create(:order_cycle, distributors: [d]) @@ -57,24 +50,31 @@ describe ShopController do describe "returning products" do let(:product) { create(:product) } - let(:order_cycle) { create(:order_cycle, distributors: [d]) } + let(:order_cycle) { create(:order_cycle, distributors: [d], coordinator: create(:distributor_enterprise)) } + before do - Spree::Product.stub(:all).and_return([product]) + exchange = Exchange.find(order_cycle.exchanges.to_enterprises(d).outgoing.first.id) + exchange.variants << product.master end it "returns products via json" do controller.stub(:current_order_cycle).and_return order_cycle xhr :get, :products response.should be_success - response.body.should_not be_empty end it "does not return products if no order_cycle is selected" do + controller.stub(:current_order_cycle).and_return nil xhr :get, :products response.status.should == 404 response.body.should be_empty end - end + it "only returns products for the current order cycle" do + controller.stub(:current_order_cycle).and_return order_cycle + xhr :get, :products + response.body.should == [product].to_json + end + end end end