diff --git a/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee index eee5bb0175..262b965373 100644 --- a/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee @@ -1,5 +1,4 @@ -#Shop.controller "OrderCycleCtrl", ($scope, OrderCycle) -> - - #$scope.setOrderCycle = ()-> - #console.log "foo" - ##OrderCycle. +Shop.controller "OrderCycleCtrl", ($scope, $rootScope, OrderCycle) -> + $scope.order_cycle = OrderCycle.order_cycle + $scope.changeOrderCycle = -> + OrderCycle.set_order_cycle() diff --git a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee index d6de5cb96e..a6ca84bc84 100644 --- a/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/products_controller.js.coffee @@ -1,4 +1,12 @@ -angular.module("Shop").controller "ProductsCtrl", ($scope, Product) -> +angular.module("Shop").controller "ProductsCtrl", ($scope, $rootScope, Product) -> $scope.products = Product.all() + #$scope.order_cycle = OrderCycle.order_cycle + #$scope.updateProducts = -> + #$scope.products = Product.all() + #$scope.$watch "order_cycle.order_cycle_id", $scope.updateProducts + #$scope.updateProducts() + + + diff --git a/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee b/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee index 2b285c64bc..680cf48631 100644 --- a/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee +++ b/app/assets/javascripts/darkswarm/services/order_cycle.js.coffee @@ -1,6 +1,10 @@ -Shop.factory 'OrderCycle', ($resource) -> - class OrderCycle - @set_order_cycle: (id)-> - new $resource("/shop/order_cycle").$save () -> - console.log "pushed" - # Push id to endpoint +Shop.factory 'OrderCycle', ($resource, Product) -> + new class OrderCycle + @order_cycle = { + order_cycle_id: null + } + + set_order_cycle: (id = null)-> + new $resource("/shop/order_cycle").save {order_cycle_id: id}, -> + Product.update() + diff --git a/app/assets/javascripts/darkswarm/services/product.js.coffee b/app/assets/javascripts/darkswarm/services/product.js.coffee index 5fc09dae54..1e328c52a7 100644 --- a/app/assets/javascripts/darkswarm/services/product.js.coffee +++ b/app/assets/javascripts/darkswarm/services/product.js.coffee @@ -1,5 +1,7 @@ Shop.factory 'Product', ($resource) -> - class Product - @all: -> - response = $resource("/shop/products").query() - + new class Product + @products: null + update: -> + @products = $resource("/shop/products").query() + all: -> + @products || @update() diff --git a/app/views/shop/_order_cycles.html.haml b/app/views/shop/_order_cycles.html.haml index f485ad3d21..1d2eb1bff8 100644 --- a/app/views/shop/_order_cycles.html.haml +++ b/app/views/shop/_order_cycles.html.haml @@ -1,4 +1,4 @@ -%ordercycle +%ordercycle{"ng-controller" => "OrderCycleCtrl"} - if @order_cycles.empty? Orders are currently closed for this hub %p Please contact your hub directly to see if they accept late orders, or wait until the next cycle opens. @@ -8,7 +8,7 @@ - else Ready for: - = select_tag :order_cycle_id, - options_for_select(order_cycles_name_and_pickup_times(@order_cycles), - current_order_cycle.andand.id), :prompt => "Select an Order Cycle!" + %select{"ng-model" => "order_cycle.order_cycle_id", + "ng-change" => "changeOrderCycle()", + "ng-options" => "c for c in #{@order_cycles.map {|oc| oc.id}.to_json}"} diff --git a/app/views/shop/show.html.haml b/app/views/shop/show.html.haml index 59fed17c5b..57c58883f7 100644 --- a/app/views/shop/show.html.haml +++ b/app/views/shop/show.html.haml @@ -7,8 +7,9 @@ = @distributor.long_description.andand.html_safe %products{"ng-controller" => "ProductsCtrl"} - {{ products }} + %pre {{ products | json }} = render partial: "enterprises/contact_us" = render partial: "enterprises/about_us" + diff --git a/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee index c01481005f..1218c83982 100644 --- a/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee @@ -1,30 +1,46 @@ -describe 'Shop controllers', -> +describe 'All controllers', -> describe 'ProductsCtrl', -> ctrl = null scope = null event = null + rootScope = null Product = null beforeEach -> module('Shop') - scope = {} Product = all: -> 'testy mctest' - inject ($controller) -> + inject ($controller, $rootScope) -> + rootScope = $rootScope + scope = $rootScope.$new() ctrl = $controller 'ProductsCtrl', {$scope: scope, Product : Product} it 'Fetches products from Product', -> expect(scope.products).toEqual 'testy mctest' + + #it "updates products when the changeOrderCycle event is seen", -> + #spyOn(scope, "updateProducts") + #rootScope.$emit "changeOrderCycle" + #expect(scope.updateProducts).toHaveBeenCalled() describe 'OrderCycleCtrl', -> ctrl = null scope = null event = null + rootScope = null + product_ctrl = null OrderCycle = null beforeEach -> module 'Shop' scope = {} - inject ($controller) -> - ctrl = $controller 'OrderCycleCtrl' + inject ($controller, $rootScope) -> + rootScope = $rootScope + scope = $rootScope.$new() + ctrl = $controller 'OrderCycleCtrl', {$scope: scope} + + #it "triggers an event when the order cycle changes", -> + #spyOn(rootScope, "$emit") + #scope.changeOrderCycle() + #expect(scope.$emit).toHaveBeenCalledWith "changeOrderCycle" diff --git a/spec/javascripts/unit/darkswarm/order_cycle_spec.js.coffee b/spec/javascripts/unit/darkswarm/order_cycle_spec.js.coffee new file mode 100644 index 0000000000..43df2d628d --- /dev/null +++ b/spec/javascripts/unit/darkswarm/order_cycle_spec.js.coffee @@ -0,0 +1,23 @@ +describe 'OrderCycle service', -> + $httpBackend = null + OrderCycle = null + mockProduct = { + update: -> + } + + beforeEach -> + module 'Shop', ($provide)-> + $provide.value "Product", mockProduct + null # IMPORTANT + # You must return null because module() is a bit dumb + inject (_OrderCycle_, _$httpBackend_)-> + $httpBackend = _$httpBackend_ + OrderCycle = _OrderCycle_ + + + it "posts the order_cycle ID and tells product to update", -> + $httpBackend.expectPOST("/shop/order_cycle", {"order_cycle_id" : 10}).respond(200) + spyOn(mockProduct, "update") + OrderCycle.set_order_cycle(10) + $httpBackend.flush() + expect(mockProduct.update).toHaveBeenCalled() diff --git a/spec/javascripts/unit/darkswarm/product_spec.js.coffee b/spec/javascripts/unit/darkswarm/product_spec.js.coffee index 05e0961ca9..1b25ded9e1 100644 --- a/spec/javascripts/unit/darkswarm/product_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/product_spec.js.coffee @@ -8,7 +8,7 @@ describe 'Product service', -> Product = $injector.get("Product") $httpBackend = _$httpBackend_ - it "Fetches products from the backend", -> + it "Fetches products from the backend on init", -> $httpBackend.expectGET("/shop/products").respond([{test : "cats"}]) products = Product.all() $httpBackend.flush() diff --git a/spec/javascripts/unit/product_spec.js.coffee b/spec/javascripts/unit/product_spec.js.coffee deleted file mode 100644 index d48d09244f..0000000000 --- a/spec/javascripts/unit/product_spec.js.coffee +++ /dev/null @@ -1,15 +0,0 @@ -describe 'Shop services', -> - $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" diff --git a/spec/javascripts/unit/shop_spec.js.coffee b/spec/javascripts/unit/shop_spec.js.coffee deleted file mode 100644 index d29c83b35c..0000000000 --- a/spec/javascripts/unit/shop_spec.js.coffee +++ /dev/null @@ -1,18 +0,0 @@ -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'