From 4b74e5035392e3f437855d3819ecc1ec6eb08c7f Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 8 Dec 2018 15:11:41 +0000 Subject: [PATCH] Make ProductsController#bulk_update work by making ProductSet#create_variant not mass-assigning the provided on_hand and on_demand values and set them after each variant is created --- app/models/spree/product_set.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/models/spree/product_set.rb b/app/models/spree/product_set.rb index d8716b821f..c91241d494 100644 --- a/app/models/spree/product_set.rb +++ b/app/models/spree/product_set.rb @@ -63,16 +63,16 @@ class Spree::ProductSet < ModelSet def update_product_master(product, attributes) return true unless attributes[:master_attributes] - update_variant(product, attributes[:master_attributes]) + create_or_update_variant(product, attributes[:master_attributes]) end def update_variants_attributes(product, variants_attributes) variants_attributes.each do |attributes| - update_variant(product, attributes) + create_or_update_variant(product, attributes) end end - def update_variant(product, variant_attributes) + def create_or_update_variant(product, variant_attributes) found_variant = product.variants_including_master.find do |variant| variant.id.to_s == variant_attributes[:id].to_s && variant.persisted? end @@ -80,10 +80,20 @@ class Spree::ProductSet < ModelSet if found_variant.present? found_variant.update_attributes(variant_attributes.except(:id)) else - product.variants.create(variant_attributes) + create_variant(product, variant_attributes) end end + def create_variant(product, variant_attributes) + on_hand = variant_attributes.delete(:on_hand) + on_demand = variant_attributes.delete(:on_demand) + + variant = product.variants.create(variant_attributes) + + variant.on_demand = on_demand if on_demand.present? + variant.on_hand = on_hand.to_i if on_hand.present? + end + def collection_attributes=(attributes) @collection = Spree::Product .where(id: attributes.each_value.map { |product| product[:id] })