From 5d30df5717f0fef6ab39c078b2a1ba05e32e38c1 Mon Sep 17 00:00:00 2001 From: Rob H Date: Fri, 21 Jun 2013 07:30:44 +0530 Subject: [PATCH] BPUR: add supplier name to product model to allow searching --- .../javascripts/admin/bulk_product_update.js | 33 +++++++++++++------ .../spree/admin/products/bulk_edit.html.haml | 2 +- app/views/spree/admin/products/bulk_index.rep | 5 ++- spec/controllers/product_controller_spec.rb | 10 ++++-- .../admin/bulk_product_update_spec.rb | 10 +++--- .../unit/bulk_product_update_spec.js | 2 ++ 6 files changed, 43 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/admin/bulk_product_update.js b/app/assets/javascripts/admin/bulk_product_update.js index 58616cab98..ca15efb621 100644 --- a/app/assets/javascripts/admin/bulk_product_update.js +++ b/app/assets/javascripts/admin/bulk_product_update.js @@ -87,8 +87,6 @@ productsApp.directive('ngToggleVariants',function(){ }); productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http, dataFetcher) { - $scope.dirtyProducts = {}; - $scope.updateStatusMessage = { text: "", style: {} @@ -102,14 +100,30 @@ productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http $scope.refreshProducts = function(){ dataFetcher('/admin/products/bulk_index.json').then(function(data){ - $scope.products = data; - $scope.displayProperties = {}; - angular.forEach($scope.products,function(product){ - $scope.displayProperties[product.id] = { showVariants: false } - }); + $scope.resetProducts(data); }); }; + $scope.resetProducts = function(data){ + $scope.products = data; + $scope.dirtyProducts = {}; + $scope.displayProperties = $scope.displayProperties || {}; + angular.forEach($scope.products,function(product){ + $scope.displayProperties[product.id] = $scope.displayProperties[product.id] || { showVariants: false }; + $scope.matchSupplier(product); + }); + } + + $scope.matchSupplier = function(product){ + for (i in $scope.suppliers){ + var supplier = $scope.suppliers[i]; + if (angular.equals(supplier,product.supplier)){ + product.supplier = supplier; + break; + } + } + } + $scope.updateOnHand = function(product){ product.on_hand = onHand(product); } @@ -174,8 +188,7 @@ productsApp.controller('AdminBulkProductsCtrl', function($scope, $timeout, $http }) .success(function(data){ if (angular.toJson($scope.products) == angular.toJson(data)){ - $scope.products = data; - $scope.dirtyProducts = {}; + $scope.resetProducts(data); $scope.displaySuccess(); } else{ @@ -273,7 +286,7 @@ function filterSubmitProducts(productsToFilter){ var hasUpdatableProperty = false; filteredProduct.id = product.id; if (product.hasOwnProperty("name")) { filteredProduct.name = product.name; hasUpdatableProperty = true; } - if (product.hasOwnProperty("supplier_id")) { filteredProduct.supplier_id = product.supplier_id; hasUpdatableProperty = true; } + if (product.hasOwnProperty("supplier")) { filteredProduct.supplier_id = product.supplier.id; hasUpdatableProperty = true; } if (product.hasOwnProperty("price")) { filteredProduct.price = product.price; hasUpdatableProperty = true; } if (product.hasOwnProperty("on_hand") && filteredVariants.length == 0) { filteredProduct.on_hand = product.on_hand; hasUpdatableProperty = true; } //only update if no variants present if (product.hasOwnProperty("available_on")) { filteredProduct.available_on = product.available_on; hasUpdatableProperty = true; } diff --git a/app/views/spree/admin/products/bulk_edit.html.haml b/app/views/spree/admin/products/bulk_edit.html.haml index 5d3664acae..cc74512ac9 100644 --- a/app/views/spree/admin/products/bulk_edit.html.haml +++ b/app/views/spree/admin/products/bulk_edit.html.haml @@ -34,7 +34,7 @@ %td.firstcol %input{ 'ng-model' => "product.name", :name => 'product_name', 'ng-track-product' => 'name', :type => 'text' } %td - %select.select2{ 'ng-model' => 'product.supplier_id', :name => 'supplier_id', 'ng-track-product' => 'supplier_id', 'ng-options' => 's.id as s.name for s in suppliers' } + %select.select2{ 'ng-model' => 'product.supplier', :name => 'supplier', 'ng-track-product' => 'supplier', 'ng-options' => 's.name for s in suppliers' } %td %input{ 'ng-model' => 'product.price', 'ng-decimal' => :true, :name => 'price', 'ng-track-product' => 'price', :type => 'text' } %td diff --git a/app/views/spree/admin/products/bulk_index.rep b/app/views/spree/admin/products/bulk_index.rep index cf850100c0..630fa16c3f 100644 --- a/app/views/spree/admin/products/bulk_index.rep +++ b/app/views/spree/admin/products/bulk_index.rep @@ -1,7 +1,10 @@ r.list_of :products, @collection.sort_by(&:id) do r.element :id r.element :name - r.element :supplier_id + r.element :supplier do + r.element :id + r.element :name + end r.element :available_on, Proc.new{ |product| product.available_on.strftime("%F %T") } r.element :price r.element :on_hand diff --git a/spec/controllers/product_controller_spec.rb b/spec/controllers/product_controller_spec.rb index 7e0adb76e0..b5cf5fe8c9 100644 --- a/spec/controllers/product_controller_spec.rb +++ b/spec/controllers/product_controller_spec.rb @@ -30,7 +30,10 @@ describe Spree::Admin::ProductsController do p1r = { "id" => p1.id, "name" => p1.name, - "supplier_id" => p1.supplier_id, + "supplier" => { + "id" => p1.supplier_id, + "name" => p1.supplier.name + }, "available_on" => p1.available_on.strftime("%F %T"), "price" => p1.price.to_s, "on_hand" => ( v11.on_hand + v12.on_hand + v13.on_hand ), @@ -44,7 +47,10 @@ describe Spree::Admin::ProductsController do p2r = { "id" => p2.id, "name" => p2.name, - "supplier_id" => p2.supplier_id, + "supplier" => { + "id" => p2.supplier_id, + "name" => p2.supplier.name + }, "available_on" => p2.available_on.strftime("%F %T"), "price" => p2.price.to_s, "on_hand" => v21.on_hand, diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index 032b18e79c..936e0fb993 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -40,8 +40,8 @@ feature %q{ visit '/admin/products/bulk_edit' - page.should have_select "supplier_id", with_options: [s1.name,s2.name,s3.name], selected: s2.name - page.should have_select "supplier_id", with_options: [s1.name,s2.name,s3.name], selected: s3.name + page.should have_select "supplier", with_options: [s1.name,s2.name,s3.name], selected: s2.name + page.should have_select "supplier", with_options: [s1.name,s2.name,s3.name], selected: s3.name end it "displays a date input for available_on for each product, formatted to yyyy-mm-dd hh:mm:ss" do @@ -183,13 +183,13 @@ feature %q{ visit '/admin/products/bulk_edit' page.should have_field "product_name", with: p.name - page.should have_select "supplier_id", selected: s1.name + page.should have_select "supplier", selected: s1.name page.should have_field "available_on", with: p.available_on.strftime("%F %T") page.should have_field "price", with: "10.0" page.should have_field "on_hand", with: "6" fill_in "product_name", with: "Big Bag Of Potatoes" - select(s2.name, :from => 'supplier_id') + select(s2.name, :from => 'supplier') fill_in "available_on", with: (Date.today-3).strftime("%F %T") fill_in "price", with: "20" fill_in "on_hand", with: "18" @@ -200,7 +200,7 @@ feature %q{ visit '/admin/products/bulk_edit' page.should have_field "product_name", with: "Big Bag Of Potatoes" - page.should have_select "supplier_id", selected: s2.name + page.should have_select "supplier", selected: s2.name page.should have_field "available_on", with: (Date.today-3).strftime("%F %T") page.should have_field "price", with: "20.0" page.should have_field "on_hand", with: "18" diff --git a/spec/javascripts/unit/bulk_product_update_spec.js b/spec/javascripts/unit/bulk_product_update_spec.js index 64552cffcc..19286c2408 100644 --- a/spec/javascripts/unit/bulk_product_update_spec.js +++ b/spec/javascripts/unit/bulk_product_update_spec.js @@ -114,6 +114,7 @@ describe("filtering products", function(){ updated_at: null, count_on_hand: 0, supplier_id: 5, + supplier: { id: 5, name: "Supplier 1" }, group_buy: null, group_buy_unit_size: null, on_demand: false, @@ -323,6 +324,7 @@ describe("AdminBulkProductsCtrl", function(){ it("deletes products with a http delete request to /admin/products/(permalink).js", function(){ spyOn(window, "confirm").andReturn(true); scope.products = [ { id: 9, permalink_live: "apples" }, { id: 13, permalink_live: "oranges" } ]; + scope.dirtyProducts = {}; httpBackend.expectDELETE('/admin/products/oranges.js').respond(200, "data"); scope.deleteProduct(scope.products[1]); httpBackend.flush();