diff --git a/app/assets/javascripts/admin/bulk_product_update.js.coffee b/app/assets/javascripts/admin/bulk_product_update.js.coffee index 12a7cda806..6274d28cfa 100644 --- a/app/assets/javascripts/admin/bulk_product_update.js.coffee +++ b/app/assets/javascripts/admin/bulk_product_update.js.coffee @@ -181,7 +181,17 @@ productsApp.controller "AdminBulkProductsCtrl", [ break $scope.updateOnHand = (product) -> - product.on_hand = onHand(product) + product.on_hand = $scope.onHand(product) + + $scope.onHand = (product) -> + onHand = 0 + if product.hasOwnProperty("variants") and product.variants instanceof Object + angular.forEach product.variants, (variant) -> + onHand = parseInt(onHand) + parseInt((if variant.on_hand > 0 then variant.on_hand else 0)) + else + onHand = "error" + onHand + $scope.editWarn = (product, variant) -> window.location = "/admin/products/" + product.permalink_live + ((if variant then "/variants/" + variant.id else "")) + "/edit" if ($scope.dirtyProductCount() > 0 and confirm("Unsaved changes will be lost. Continue anyway?")) or ($scope.dirtyProductCount() is 0) @@ -298,17 +308,6 @@ productsApp.factory "dataFetcher", [ ] -onHand = (product) -> - onHand = 0 - if product.hasOwnProperty("variants") and product.variants instanceof Object - angular.forEach product.variants, (variant) -> - onHand = parseInt(onHand) + parseInt((if variant.on_hand > 0 then variant.on_hand else 0)) - - else - onHand = "error" - onHand - - filterSubmitProducts = (productsToFilter) -> filteredProducts = [] if productsToFilter instanceof Object diff --git a/spec/javascripts/unit/bulk_product_update_spec.js b/spec/javascripts/unit/bulk_product_update_spec.js index cb110d56e3..7cd8fc98ac 100644 --- a/spec/javascripts/unit/bulk_product_update_spec.js +++ b/spec/javascripts/unit/bulk_product_update_spec.js @@ -266,6 +266,17 @@ describe("AdminBulkProductsCtrl", function(){ }); describe("getting on_hand counts when products have variants", function(){ + beforeEach(function(){ + module('bulk_product_update'); + }); + + beforeEach(inject(function($controller,$rootScope) { + scope = $rootScope.$new(); + ctrl = $controller; + + ctrl('AdminBulkProductsCtrl', { $scope: scope } ); + })); + var p1, p2, p3; beforeEach(function(){ p1 = { variants: { 1: { id: 1, on_hand: 1 }, 2: { id: 2, on_hand: 2 }, 3: { id: 3, on_hand: 3 } } }; @@ -274,20 +285,20 @@ describe("AdminBulkProductsCtrl", function(){ }); it("sums variant on_hand properties", function(){ - expect(onHand(p1)).toEqual(6); + expect(scope.onHand(p1)).toEqual(6); }); it("ignores items in variants without an on_hand property (adds 0)", function(){ - expect(onHand(p2)).toEqual(5); + expect(scope.onHand(p2)).toEqual(5); }); it("ignores on_hand properties of objects in arrays which are not named 'variants' (adds 0)", function(){ - expect(onHand(p3)).toEqual(3); + expect(scope.onHand(p3)).toEqual(3); }); it("returns 'error' if not given an object with a variants property that is an object", function(){ - expect( onHand([]) ).toEqual('error'); - expect( onHand( { not_variants: [] } ) ).toEqual('error'); + expect( scope.onHand([]) ).toEqual('error'); + expect( scope.onHand( { not_variants: [] } ) ).toEqual('error'); }); });