Getting an order cycle update function in place like a boss

This commit is contained in:
Will Marshall
2013-12-11 12:42:02 +11:00
parent 63dfa0b696
commit 4db8f755bf
5 changed files with 76 additions and 0 deletions

View File

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

View File

@@ -4,6 +4,7 @@ Openfoodnetwork::Application.routes.draw do
resource :shop, controller: :shop do
get :products
post :order_cycle
end
resources :enterprises do

View File

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

View File

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

View File

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