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

This commit is contained in:
luisramos0
2018-12-08 23:00:02 +00:00
parent 66e69edca4
commit 47be452ca0

View File

@@ -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