From 1d4198d402a72616f3a61017232c70cb5a52650d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 30 Sep 2013 09:46:39 +1000 Subject: [PATCH] Removing a supplier exchange removes variants from distribution --- .../admin/order_cycle.js.erb.coffee | 16 ++++++++ .../order_cycles/_exchange_form.html.haml | 3 +- app/views/admin/order_cycles/_form.html.haml | 4 +- .../unit/order_cycle_spec.js.coffee | 38 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/admin/order_cycle.js.erb.coffee b/app/assets/javascripts/admin/order_cycle.js.erb.coffee index 2135afceeb..be6696af70 100644 --- a/app/assets/javascripts/admin/order_cycle.js.erb.coffee +++ b/app/assets/javascripts/admin/order_cycle.js.erb.coffee @@ -42,6 +42,10 @@ angular.module('order_cycle', ['ngResource']) $event.preventDefault() OrderCycle.addDistributor($scope.new_distributor_id) + $scope.removeExchange = ($event, exchange) -> + $event.preventDefault() + OrderCycle.removeExchange(exchange) + $scope.addCoordinatorFee = ($event) -> $event.preventDefault() OrderCycle.addCoordinatorFee() @@ -109,6 +113,10 @@ angular.module('order_cycle', ['ngResource']) $event.preventDefault() OrderCycle.addDistributor($scope.new_distributor_id) + $scope.removeExchange = ($event, exchange) -> + $event.preventDefault() + OrderCycle.removeExchange(exchange) + $scope.addCoordinatorFee = ($event) -> $event.preventDefault() OrderCycle.addCoordinatorFee() @@ -165,6 +173,14 @@ angular.module('order_cycle', ['ngResource']) addDistributor: (new_distributor_id) -> this.order_cycle.outgoing_exchanges.push({enterprise_id: new_distributor_id, active: true, variants: {}, enterprise_fees: []}) + removeExchange: (exchange) -> + incoming_index = this.order_cycle.incoming_exchanges.indexOf exchange + this.order_cycle.incoming_exchanges.splice(incoming_index, 1) if incoming_index > -1 + outgoing_index = this.order_cycle.outgoing_exchanges.indexOf exchange + this.order_cycle.outgoing_exchanges.splice(outgoing_index, 1) if outgoing_index > -1 + + this.removeDistributionOfVariant(variant_id) for variant_id, active of exchange.variants when active + addCoordinatorFee: -> this.order_cycle.coordinator_fees.push({}) diff --git a/app/views/admin/order_cycles/_exchange_form.html.haml b/app/views/admin/order_cycles/_exchange_form.html.haml index ab72dcf5c0..65994ba543 100644 --- a/app/views/admin/order_cycles/_exchange_form.html.haml +++ b/app/views/admin/order_cycles/_exchange_form.html.haml @@ -1,4 +1,3 @@ -%td.active= check_box_tag 'order_cycle_exchange_{{ $index }}_active', 1, 1, 'ng-model' => 'exchange.active', 'id' => 'order_cycle_exchange_{{ $index }}_active' %td{:class => "#{type}_name"} {{ enterprises[exchange.enterprise_id].name }} %td.products = f.submit 'Products', 'ng-click' => 'toggleProducts($event, exchange)' @@ -23,3 +22,5 @@ = link_to 'Remove', '#', {'id' => 'order_cycle_{{ exchangeDirection(exchange) }}_exchange_{{ $parent.$index }}_enterprise_fees_{{ $index }}_remove', 'ng-click' => 'removeExchangeFee($event, exchange, $index)'} = f.submit 'Add fee', 'ng-click' => 'addExchangeFee($event, exchange)' +%td.actions + %a{'ng-click' => 'removeExchange($event, exchange)', :class => "icon-trash no-text"} diff --git a/app/views/admin/order_cycles/_form.html.haml b/app/views/admin/order_cycles/_form.html.haml index 70e119c167..431e142b3c 100644 --- a/app/views/admin/order_cycles/_form.html.haml +++ b/app/views/admin/order_cycles/_form.html.haml @@ -15,10 +15,10 @@ %table.exchanges %thead %tr - %th %th Supplier %th Products %th Fees + %th.actions %tbody{'ng-repeat' => 'exchange in order_cycle.incoming_exchanges'} %tr{'class' => "supplier supplier-{{ exchange.enterprise_id }}"} = render 'exchange_form', :f => f, :type => 'supplier' @@ -39,11 +39,11 @@ %table.exchanges %thead %tr - %th %th Distributor %th Products %th Collection details %th Fees + %th.actions %tbody{'ng-repeat' => 'exchange in order_cycle.outgoing_exchanges'} %tr{'class' => "distributor distributor-{{ exchange.enterprise_id }}"} = render 'exchange_form', :f => f, :type => 'distributor' diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index f12d3f972e..2a72eea70f 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -21,6 +21,7 @@ describe 'OrderCycle controllers', -> toggleProducts: jasmine.createSpy('toggleProducts') addSupplier: jasmine.createSpy('addSupplier') addDistributor: jasmine.createSpy('addDistributor') + removeExchange: jasmine.createSpy('removeExchange') addCoordinatorFee: jasmine.createSpy('addCoordinatorFee') removeCoordinatorFee: jasmine.createSpy('removeCoordinatorFee') addExchangeFee: jasmine.createSpy('addExchangeFee') @@ -102,6 +103,11 @@ describe 'OrderCycle controllers', -> expect(event.preventDefault).toHaveBeenCalled() expect(OrderCycle.addDistributor).toHaveBeenCalledWith('new distributor id') + it 'Removes order cycle exchanges', -> + scope.removeExchange(event, 'exchange') + expect(event.preventDefault).toHaveBeenCalled() + expect(OrderCycle.removeExchange).toHaveBeenCalledWith('exchange') + it 'Adds coordinator fees', -> scope.addCoordinatorFee(event) expect(event.preventDefault).toHaveBeenCalled() @@ -155,6 +161,7 @@ describe 'OrderCycle controllers', -> toggleProducts: jasmine.createSpy('toggleProducts') addSupplier: jasmine.createSpy('addSupplier') addDistributor: jasmine.createSpy('addDistributor') + removeExchange: jasmine.createSpy('removeExchange') addCoordinatorFee: jasmine.createSpy('addCoordinatorFee') removeCoordinatorFee: jasmine.createSpy('removeCoordinatorFee') addExchangeFee: jasmine.createSpy('addExchangeFee') @@ -235,6 +242,11 @@ describe 'OrderCycle controllers', -> expect(event.preventDefault).toHaveBeenCalled() expect(OrderCycle.addDistributor).toHaveBeenCalledWith('new distributor id') + it 'Removes order cycle exchanges', -> + scope.removeExchange(event, 'exchange') + expect(event.preventDefault).toHaveBeenCalled() + expect(OrderCycle.removeExchange).toHaveBeenCalledWith('exchange') + it 'Adds coordinator fees', -> scope.addCoordinatorFee(event) expect(event.preventDefault).toHaveBeenCalled() @@ -421,6 +433,32 @@ describe 'OrderCycle services', -> {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} ] + describe 'removing exchanges', -> + it 'removes incoming exchanges', -> + exchange = {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} + OrderCycle.order_cycle.incoming_exchanges = [exchange] + OrderCycle.removeExchange(exchange) + expect(OrderCycle.order_cycle.incoming_exchanges).toEqual [] + + it 'removes outgoing exchanges', -> + exchange = {enterprise_id: '123', active: true, variants: {}, enterprise_fees: []} + OrderCycle.order_cycle.outgoing_exchanges = [exchange] + OrderCycle.removeExchange(exchange) + expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [] + + it 'removes distribution of all exchange variants', -> + spyOn(OrderCycle, 'removeDistributionOfVariant') + exchange = + enterprise_id: '123' + active: true + variants: {1: true, 2: false, 3: true} + enterprise_fees: [] + OrderCycle.order_cycle.incoming_exchanges = [exchange] + OrderCycle.removeExchange(exchange) + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('1') + expect(OrderCycle.removeDistributionOfVariant).not.toHaveBeenCalledWith('2') + expect(OrderCycle.removeDistributionOfVariant).toHaveBeenCalledWith('3') + it 'adds coordinator fees', -> OrderCycle.addCoordinatorFee() expect(OrderCycle.order_cycle.coordinator_fees).toEqual [{}]