From dbfcbc6db903386474a57fd35bf2dfbc1825be21 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 1 Feb 2019 16:51:52 +1100 Subject: [PATCH] Apply import defaults to variants of new products Spree 2 doesn't have `Product#on_hand=` any more. We actually can't save stock levels without saving the product first which creates a stock item. This hacky solution overwrites the attribute in the entry so that it gets copied to the variant later on. I think a better solution needs some refactoring as proposed in: https://github.com/openfoodfoundation/openfoodnetwork/issues/2783#issuecomment-459601530 --- app/models/product_import/entry_processor.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 6286646023..9954bf9c6c 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -212,12 +212,20 @@ module ProductImport case setting['mode'] when 'overwrite_all' object.assign_attributes(attribute => setting['value']) + # In case of new products, some attributes are saved on the variant. + # We write them to the entry here to be copied to the variant later. + if entry.respond_to? "#{attribute}=" + entry.public_send("#{attribute}=", setting['value']) + end when 'overwrite_empty' if object.public_send(attribute).blank? || ((attribute == 'on_hand' || attribute == 'count_on_hand') && entry.on_hand_nil) object.assign_attributes(attribute => setting['value']) + if entry.respond_to? "#{attribute}=" + entry.public_send("#{attribute}=", setting['value']) + end end end end