diff --git a/app/controllers/shop_controller.rb b/app/controllers/shop_controller.rb index 5d3f8caf02..1862dfd0c6 100644 --- a/app/controllers/shop_controller.rb +++ b/app/controllers/shop_controller.rb @@ -15,6 +15,15 @@ class ShopController < BaseController end end + def order_cycle + if oc = OrderCycle.with_distributor(@distributor).active.find_by_id(params[:order_cycle_id]) + current_order(true).set_order_cycle! oc + render status: 200, json: "" + else + render status: 404, json: "" + end + end + private def set_distributor diff --git a/config/routes.rb b/config/routes.rb index 7991f605ff..7a67be8d06 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Openfoodnetwork::Application.routes.draw do resource :shop, controller: :shop do get :products + post :order_cycle end resources :enterprises do diff --git a/spec/controllers/shop_controller_spec.rb b/spec/controllers/shop_controller_spec.rb index 49257e5de0..7dfc43d289 100644 --- a/spec/controllers/shop_controller_spec.rb +++ b/spec/controllers/shop_controller_spec.rb @@ -19,8 +19,29 @@ describe ShopController do spree_get :show controller.current_order_cycle.should == nil end + + it "should allow the user to post to select the current order cycle" do + oc1 = create(:order_cycle, distributors: [d]) + oc2 = create(:order_cycle, distributors: [d]) + + spree_post :order_cycle, order_cycle_id: oc2.id + response.should be_success + controller.current_order_cycle.should == oc2 + end + + it "should not allow the user to select an invalid order cycle" do + oc1 = create(:order_cycle, distributors: [d]) + oc2 = create(:order_cycle, distributors: [d]) + oc3 = create(:order_cycle, distributors: [create(:distributor_enterprise)]) + + spree_post :order_cycle, order_cycle_id: oc3.id + response.status.should == 404 + controller.current_order_cycle.should == nil + + end end + describe "returning products" do let(:product) { create(:product) } let(:order_cycle) { create(:order_cycle, distributors: [d]) } diff --git a/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee new file mode 100644 index 0000000000..c01481005f --- /dev/null +++ b/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee @@ -0,0 +1,30 @@ +describe 'Shop controllers', -> + describe 'ProductsCtrl', -> + ctrl = null + scope = null + event = null + Product = null + + beforeEach -> + module('Shop') + scope = {} + Product = + all: -> + 'testy mctest' + inject ($controller) -> + ctrl = $controller 'ProductsCtrl', {$scope: scope, Product : Product} + + it 'Fetches products from Product', -> + expect(scope.products).toEqual 'testy mctest' + + describe 'OrderCycleCtrl', -> + ctrl = null + scope = null + event = null + OrderCycle = null + + beforeEach -> + module 'Shop' + scope = {} + inject ($controller) -> + ctrl = $controller 'OrderCycleCtrl' diff --git a/spec/javascripts/unit/darkswarm/product_spec.js.coffee b/spec/javascripts/unit/darkswarm/product_spec.js.coffee new file mode 100644 index 0000000000..05e0961ca9 --- /dev/null +++ b/spec/javascripts/unit/darkswarm/product_spec.js.coffee @@ -0,0 +1,15 @@ +describe 'Product service', -> + $httpBackend = null + Product = null + + beforeEach -> + module 'Shop' + inject ($injector, _$httpBackend_)-> + Product = $injector.get("Product") + $httpBackend = _$httpBackend_ + + it "Fetches products from the backend", -> + $httpBackend.expectGET("/shop/products").respond([{test : "cats"}]) + products = Product.all() + $httpBackend.flush() + expect(products[0].test).toEqual "cats"