From 47be452ca0850fccee3242b4224af8f78fecf9ae Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 8 Dec 2018 23:00:02 +0000 Subject: [PATCH] Make ProductsController#create and #update work by not mass-assigning the provided on_hand and on_demand values and set them in product variant after the product is created --- .../admin/products_controller_decorator.rb | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 3ee88fc18d..c9d0f7c36d 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -31,6 +31,18 @@ Spree::Admin::ProductsController.class_eval do @show_latest_import = params[:latest_import] || false end + def create + delete_stock_params_and_set_after do + super + end + end + + def update + delete_stock_params_and_set_after do + super + end + end + def bulk_update collection_hash = Hash[params[:products].each_with_index.map { |p,i| [i,p] }] product_set = Spree::ProductSet.new({:collection_attributes => collection_hash}) @@ -119,4 +131,22 @@ Spree::Admin::ProductsController.class_eval do end end end + + def delete_stock_params_and_set_after + on_demand = params[:product].delete(:on_demand) + on_hand = params[:product].delete(:on_hand) + + yield + + set_stock_levels(@product, on_hand, on_demand) if @product.valid? + end + + def set_stock_levels(product, on_hand, on_demand) + variant = product.master + if product.variants.any? + variant = product.variants.first + end + variant.on_demand = on_demand if on_demand.present? + variant.on_hand = on_hand.to_i if on_hand.present? + end end