From 8ff749d0ebb7982d596d269395a2f1c90b16afb6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 13 Aug 2013 16:29:27 +1000 Subject: [PATCH] Find enterprises participating in an order cycle --- .../admin/order_cycle.js.erb.coffee | 11 +++++++ .../unit/order_cycle_spec.js.coffee | 29 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index d2af6a2994..4b416c99d8 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -21,6 +21,9 @@ angular.module('order_cycle', ['ngResource']) $scope.incomingExchangesVariants = -> OrderCycle.incomingExchangesVariants() + $scope.participatingEnterprises = -> + $scope.enterprises[id] for id in OrderCycle.participatingEnterpriseIds() + $scope.toggleProducts = ($event, exchange) -> $event.preventDefault() OrderCycle.toggleProducts(exchange) @@ -79,6 +82,9 @@ angular.module('order_cycle', ['ngResource']) $scope.incomingExchangesVariants = -> OrderCycle.incomingExchangesVariants() + $scope.participatingEnterprises = -> + $scope.enterprises[id] for id in OrderCycle.participatingEnterpriseIds() + $scope.toggleProducts = ($event, exchange) -> $event.preventDefault() OrderCycle.toggleProducts(exchange) @@ -178,6 +184,11 @@ angular.module('order_cycle', ['ngResource']) variant_ids.push(parseInt(id)) for id, active of exchange.variants when active variant_ids + participatingEnterpriseIds: -> + suppliers = (exchange.enterprise_id for exchange in this.order_cycle.incoming_exchanges) + distributors = (exchange.enterprise_id for exchange in this.order_cycle.outgoing_exchanges) + jQuery.unique(suppliers.concat(distributors)).sort() + load: (order_cycle_id) -> service = this diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index 2d3bae5c36..7e2f040a97 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -66,6 +66,15 @@ describe 'OrderCycle controllers', -> expect(scope.variantSuppliedToOrderCycle('variant')).toEqual('variant supplied') expect(OrderCycle.variantSuppliedToOrderCycle).toHaveBeenCalledWith('variant') + it 'Finds enterprises participating in the order cycle', -> + scope.enterprises = + 1: {id: 1, name: 'Eaterprises'} + 2: {id: 2, name: 'Pepper Tree Place'} + OrderCycle.participatingEnterpriseIds = jasmine.createSpy('participatingEnterpriseIds').andReturn([2]) + expect(scope.participatingEnterprises()).toEqual([ + {id: 2, name: 'Pepper Tree Place'} + ]) + it 'Delegates toggleProducts to OrderCycle', -> scope.toggleProducts(event, 'exchange') expect(event.preventDefault).toHaveBeenCalled() @@ -180,6 +189,15 @@ describe 'OrderCycle controllers', -> expect(scope.variantSuppliedToOrderCycle('variant')).toEqual('variant supplied') expect(OrderCycle.variantSuppliedToOrderCycle).toHaveBeenCalledWith('variant') + it 'Finds enterprises participating in the order cycle', -> + scope.enterprises = + 1: {id: 1, name: 'Eaterprises'} + 2: {id: 2, name: 'Pepper Tree Place'} + OrderCycle.participatingEnterpriseIds = jasmine.createSpy('participatingEnterpriseIds').andReturn([2]) + expect(scope.participatingEnterprises()).toEqual([ + {id: 2, name: 'Pepper Tree Place'} + ]) + it 'Delegates toggleProducts to OrderCycle', -> scope.toggleProducts(event, 'exchange') expect(event.preventDefault).toHaveBeenCalled() @@ -406,6 +424,17 @@ describe 'OrderCycle services', -> {id: 3} ] + it 'finds participating enterprise ids', -> + OrderCycle.order_cycle.incoming_exchanges = [ + {enterprise_id: 1} + {enterprise_id: 2} + ] + OrderCycle.order_cycle.outgoing_exchanges = [ + {enterprise_id: 2} + {enterprise_id: 3} + ] + expect(OrderCycle.participatingEnterpriseIds()).toEqual [1, 2, 3] + describe 'fetching all variants supplied on incoming exchanges', -> it 'collects variants from incoming exchanges', -> OrderCycle.order_cycle.incoming_exchanges = [