From fefea54375b490c79355ff1eff8264bed9af61b8 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 4 Dec 2013 16:34:11 +1100 Subject: [PATCH] Do not include variant_unit_with_scale in product update check. When saving products, save variant_unit and variant_unit_scale. --- .../admin/bulk_product_update.js.coffee | 15 +++++++- .../unit/bulk_product_update_spec.js.coffee | 37 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/bulk_product_update.js.coffee b/app/assets/javascripts/admin/bulk_product_update.js.coffee index 62b38a6ff5..399a08e90a 100644 --- a/app/assets/javascripts/admin/bulk_product_update.js.coffee +++ b/app/assets/javascripts/admin/bulk_product_update.js.coffee @@ -252,7 +252,7 @@ productsApp.controller "AdminBulkProductsCtrl", [ url: "/admin/products/bulk_update" data: productsToSubmit ).success((data) -> - if angular.toJson($scope.products) == angular.toJson(data) + if angular.toJson($scope.productsWithoutDerivedAttributes()) == angular.toJson(data) $scope.resetProducts data $scope.displaySuccess() else @@ -266,6 +266,14 @@ productsApp.controller "AdminBulkProductsCtrl", [ $scope.updateProducts productsToSubmit + $scope.productsWithoutDerivedAttributes = -> + products = [] + if $scope.products + products.push angular.extend {}, product for product in $scope.products + angular.forEach products, (product) -> + delete product.variant_unit_with_scale + products + $scope.setMessage = (model, text, style, timeout) -> model.text = text model.style = style @@ -354,6 +362,11 @@ filterSubmitProducts = (productsToFilter) -> if product.hasOwnProperty("price") filteredProduct.price = product.price hasUpdatableProperty = true + if product.hasOwnProperty("variant_unit_with_scale") + match = product.variant_unit_with_scale.match(/^([^_]+)_([\d\.]+)$/) + filteredProduct.variant_unit = match[1] + filteredProduct.variant_unit_scale = parseFloat(match[2]) + hasUpdatableProperty = true if product.hasOwnProperty("on_hand") and filteredVariants.length == 0 #only update if no variants present filteredProduct.on_hand = product.on_hand hasUpdatableProperty = true diff --git a/spec/javascripts/unit/bulk_product_update_spec.js.coffee b/spec/javascripts/unit/bulk_product_update_spec.js.coffee index b09ef40baf..7c2f8124f6 100644 --- a/spec/javascripts/unit/bulk_product_update_spec.js.coffee +++ b/spec/javascripts/unit/bulk_product_update_spec.js.coffee @@ -128,9 +128,21 @@ describe "filtering products", -> price: 10 ] + it 'returns variant_unit_with_scale as variant_unit and variant_unit_scale', -> + testProduct = + id: 1 + variant_unit: 'weight' + variant_unit_scale: 1 + variant_unit_with_scale: 'volume_1000' + + expect(filterSubmitProducts([testProduct])).toEqual [ + id: 1 + variant_unit: 'volume' + variant_unit_scale: 1000 + ] # TODO Not an exhaustive test, is there a better way to do this? - it "only returns properties the properties of products which ought to be updated", -> + it "only returns the properties of products which ought to be updated", -> testProduct = id: 1 name: "TestProduct" @@ -158,11 +170,17 @@ describe "filtering products", -> on_hand: 2 price: 10.0 ] + variant_unit: 'volume' + variant_unit_scale: 1 + variant_unit_name: null + variant_unit_with_scale: 'weight_1000' expect(filterSubmitProducts([testProduct])).toEqual [ id: 1 name: "TestProduct" supplier_id: 5 + variant_unit: 'weight' + variant_unit_scale: 1000 available_on: new Date() variants_attributes: [ id: 1 @@ -540,6 +558,23 @@ describe "AdminBulkProductsCtrl", -> expect(scope.displayFailure).toHaveBeenCalled() + describe 'fetching products without derived attributes', -> + beforeEach -> + ctrl "AdminBulkProductsCtrl", + $scope: scope + + it 'returns products without the variant_unit_with_scale field', -> + scope.products = [{id: 123, variant_unit_with_scale: 'weight_1000'}] + expect(scope.productsWithoutDerivedAttributes()).toEqual([{id: 123}]) + + it 'returns an empty array when products are undefined', -> + expect(scope.productsWithoutDerivedAttributes()).toEqual([]) + + it 'does not alter products', -> + scope.products = [{id: 123, variant_unit_with_scale: 'weight_1000'}] + scope.productsWithoutDerivedAttributes() + expect(scope.products).toEqual [{id: 123, variant_unit_with_scale: 'weight_1000'}] + describe "deleting products", -> beforeEach ->