diff --git a/app/assets/javascripts/admin/bulk_product_update.js b/app/assets/javascripts/admin/bulk_product_update.js index 047937f58f..024aa462ef 100644 --- a/app/assets/javascripts/admin/bulk_product_update.js +++ b/app/assets/javascripts/admin/bulk_product_update.js @@ -1,3 +1,9 @@ +var productsApp = angular.module('bulk_product_update', []) + +productsApp.config(["$httpProvider", function(provider) { + provider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content'); +}]); + function AdminBulkProductsCtrl($scope, $timeout, $http) { $scope.refreshSuppliers = function(){ $http.get('/enterprises/suppliers.json').success(function(data) { @@ -24,8 +30,7 @@ function AdminBulkProductsCtrl($scope, $timeout, $http) { $http({ method: 'POST', url: '/admin/products/bulk_update', - data: productsToSubmit, - headers: {'Content-Type': 'application/json', 'Accept': 'application/json'} + data: productsToSubmit }) .success(function(data){ if (angular.toJson($scope.products) == angular.toJson(data)){ @@ -67,8 +72,6 @@ function AdminBulkProductsCtrl($scope, $timeout, $http) { } } -var productsApp = angular.module('bulk_product_update', []) - function sortByID(array){ var sortedArray = []; for (var i in array){ diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 6df7ac1914..c16a2d384c 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -10,6 +10,18 @@ Spree::Admin::ProductsController.class_eval do end end + def bulk_update + @collection_hash = Hash[params[:_json].each_with_index.map { |p,i| [i,p] }] + @collection_attributes = { :collection_attributes => @collection_hash } + @product_set = Spree::ProductSet.new(@collection_attributes) + + if @product_set.save + redirect_to bulk_index_admin_products_url :format => :json + else + render :nothing => true + end + end + protected def location_after_save if URI(request.referer).path == '/admin/products/bulk_index' diff --git a/config/routes.rb b/config/routes.rb index 004dd7aa87..bf8690725f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,6 +35,7 @@ Spree::Core::Engine.routes.prepend do namespace :admin do resources :products do get :bulk_index, :on => :collection, :as => :bulk_index + post :bulk_update, :on => :collection, :as => :bulk_update end end diff --git a/spec/features/admin/bulk_product_update_spec.rb b/spec/features/admin/bulk_product_update_spec.rb index 456d6f9919..8d747da100 100644 --- a/spec/features/admin/bulk_product_update_spec.rb +++ b/spec/features/admin/bulk_product_update_spec.rb @@ -64,7 +64,7 @@ feature %q{ fill_in "product_name", with: "Big Bag Of Potatoes" click_button 'Update' - page.find("div#update-status-message").should have_content "Updating complete" + page.find("span#update-status-message").should have_content "Update complete" click_link 'Bulk Product Edit' page.should have_field "product_name", with: "Big Bag Of Potatoes"