From c7c70252d02e640bb2dc84da255c805f83e029ac Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 11 Feb 2014 19:13:11 +1100 Subject: [PATCH] ofnTrackProduct and ofnTrackVariant accept nested properties as arguments --- .../admin/bulk_product_update.js.coffee | 25 +++++++++---------- .../unit/bulk_product_update_spec.js.coffee | 13 +++++++--- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/admin/bulk_product_update.js.coffee b/app/assets/javascripts/admin/bulk_product_update.js.coffee index 81ad95f9ca..ce198c4a74 100644 --- a/app/assets/javascripts/admin/bulk_product_update.js.coffee +++ b/app/assets/javascripts/admin/bulk_product_update.js.coffee @@ -20,30 +20,31 @@ productEditModule.directive "ofnDecimal", -> viewValue -productEditModule.directive "ofnTrackProduct", -> +productEditModule.directive "ofnTrackProduct", ['$parse', ($parse) -> require: "ngModel" link: (scope, element, attrs, ngModel) -> - property_name = attrs.ofnTrackProduct ngModel.$parsers.push (viewValue) -> if ngModel.$dirty - addDirtyProperty scope.dirtyProducts, scope.product.id, property_name, viewValue + parsedPropertyName = $parse(attrs.ofnTrackProduct) + addDirtyProperty scope.dirtyProducts, scope.product.id, parsedPropertyName, viewValue scope.displayDirtyProducts() viewValue + ] -productEditModule.directive "ofnTrackVariant", -> +productEditModule.directive "ofnTrackVariant", ['$parse', ($parse) -> require: "ngModel" link: (scope, element, attrs, ngModel) -> - property_name = attrs.ofnTrackVariant ngModel.$parsers.push (viewValue) -> dirtyVariants = {} dirtyVariants = scope.dirtyProducts[scope.product.id].variants if scope.dirtyProducts.hasOwnProperty(scope.product.id) and scope.dirtyProducts[scope.product.id].hasOwnProperty("variants") if ngModel.$dirty - addDirtyProperty dirtyVariants, scope.variant.id, property_name, viewValue - addDirtyProperty scope.dirtyProducts, scope.product.id, "variants", dirtyVariants + parsedPropertyName = $parse(attrs.ofnTrackVariant) + addDirtyProperty dirtyVariants, scope.variant.id, parsedPropertyName, viewValue + addDirtyProperty scope.dirtyProducts, scope.product.id, $parse("variants"), dirtyVariants scope.displayDirtyProducts() viewValue - + ] productEditModule.directive "ofnToggleVariants", -> link: (scope, element, attrs) -> @@ -515,13 +516,11 @@ filterSubmitProducts = (productsToFilter) -> filteredProducts -addDirtyProperty = (dirtyObjects, objectID, propertyName, propertyValue) -> - if dirtyObjects.hasOwnProperty(objectID) - dirtyObjects[objectID][propertyName] = propertyValue - else +addDirtyProperty = (dirtyObjects, objectID, parsedPropertyName, propertyValue) -> + if !dirtyObjects.hasOwnProperty(objectID) dirtyObjects[objectID] = {} dirtyObjects[objectID]["id"] = objectID - dirtyObjects[objectID][propertyName] = propertyValue + parsedPropertyName.assign(dirtyObjects[objectID], propertyValue) removeCleanProperty = (dirtyObjects, objectID, propertyName) -> diff --git a/spec/javascripts/unit/bulk_product_update_spec.js.coffee b/spec/javascripts/unit/bulk_product_update_spec.js.coffee index 9fe79278d4..4381bec9e1 100644 --- a/spec/javascripts/unit/bulk_product_update_spec.js.coffee +++ b/spec/javascripts/unit/bulk_product_update_spec.js.coffee @@ -202,10 +202,17 @@ describe "filtering products for submission to database", -> describe "Maintaining a live record of dirty products and properties", -> + parse = null + beforeEach -> + module "ofn.bulk_product_edit" + beforeEach inject(($parse) -> + parse = $parse + ) + describe "adding product properties to the dirtyProducts object", -> # Applies to both products and variants it "adds the product and the property to the list if property is dirty", -> dirtyProducts = {} - addDirtyProperty dirtyProducts, 1, "name", "Product 1" + addDirtyProperty dirtyProducts, 1, parse("name"), "Product 1" expect(dirtyProducts).toEqual 1: id: 1 name: "Product 1" @@ -216,7 +223,7 @@ describe "Maintaining a live record of dirty products and properties", -> id: 1 notaname: "something" - addDirtyProperty dirtyProducts, 1, "name", "Product 3" + addDirtyProperty dirtyProducts, 1, parse("name"), "Product 3" expect(dirtyProducts).toEqual 1: id: 1 notaname: "something" @@ -228,7 +235,7 @@ describe "Maintaining a live record of dirty products and properties", -> id: 1 name: "Product 1" - addDirtyProperty dirtyProducts, 1, "name", "Product 2" + addDirtyProperty dirtyProducts, 1, parse("name"), "Product 2" expect(dirtyProducts).toEqual 1: id: 1 name: "Product 2"