diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 085df93ad1..5bdb0f6b4d 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -85,6 +85,10 @@ module ProductImport end end + def strategy + @strategy ||= strategy_factory.new + end + def total_saved_count @products_created + @variants_created + @variants_updated + @inventory_created + @inventory_updated end diff --git a/app/models/product_import/inventory_reset.rb b/app/models/product_import/inventory_reset.rb index 136a5856ee..7bc9b45dd5 100644 --- a/app/models/product_import/inventory_reset.rb +++ b/app/models/product_import/inventory_reset.rb @@ -1,17 +1,23 @@ module ProductImport class InventoryReset - def initialize(updated_ids, supplier_ids) - @updated_ids = updated_ids - @supplier_ids = supplier_ids + attr_reader :supplier_ids + + def initialize + @supplier_ids = [] end - def reset + def <<(values) + @supplier_ids << values + end + + def reset(updated_ids, _supplier_ids) + @updated_ids = updated_ids relation.update_all(count_on_hand: 0) end private - attr_reader :updated_ids, :supplier_ids + attr_reader :updated_ids def relation VariantOverride.where( diff --git a/app/models/product_import/products_reset.rb b/app/models/product_import/products_reset.rb index fd93c3b684..d3580bf2a7 100644 --- a/app/models/product_import/products_reset.rb +++ b/app/models/product_import/products_reset.rb @@ -1,17 +1,23 @@ module ProductImport class ProductsReset - def initialize(updated_ids, supplier_ids) - @updated_ids = updated_ids - @supplier_ids = supplier_ids + attr_reader :supplier_ids + + def initialize + @supplier_ids = [] end - def reset + def <<(values) + @supplier_ids << values + end + + def reset(updated_ids, _supplier_ids) + @updated_ids = updated_ids relation.update_all(count_on_hand: 0) end private - attr_reader :updated_ids, :supplier_ids + attr_reader :updated_ids def relation Spree::Variant diff --git a/app/models/product_import/reset_absent.rb b/app/models/product_import/reset_absent.rb index 94bdc6d997..0c7526f7b0 100644 --- a/app/models/product_import/reset_absent.rb +++ b/app/models/product_import/reset_absent.rb @@ -10,9 +10,6 @@ module ProductImport @settings = settings @strategy_factory = strategy_factory - - @suppliers_to_reset_products = [] - @suppliers_to_reset_inventories = [] end # For selected enterprises; set stock to zero for all products/inventory @@ -21,14 +18,10 @@ module ProductImport settings.enterprises_to_reset.each do |enterprise_id| next unless permission_by_id?(enterprise_id) - if importing_into_inventory? - @suppliers_to_reset_inventories << enterprise_id.to_i - else - @suppliers_to_reset_products << enterprise_id.to_i - end + strategy << enterprise_id.to_i end - reset_stock if suppliers_to_reset? + reset_stock if strategy.supplier_ids end private @@ -36,24 +29,10 @@ module ProductImport attr_reader :settings, :strategy_factory def reset_stock - @products_reset_count += strategy.reset - end - - def strategy - strategy_factory.new(settings.updated_ids, supplier_ids) - end - - def supplier_ids - if @suppliers_to_reset_inventories.present? - @suppliers_to_reset_inventories - elsif @suppliers_to_reset_products.present? - @suppliers_to_reset_products - end - end - - def suppliers_to_reset? - @suppliers_to_reset_inventories.present? || - @suppliers_to_reset_products.present? + @products_reset_count += strategy.reset( + settings.updated_ids, + strategy.supplier_ids + ) end end end diff --git a/spec/models/product_import/reset_absent_spec.rb b/spec/models/product_import/reset_absent_spec.rb index dd965a9907..cd85a7dc4a 100644 --- a/spec/models/product_import/reset_absent_spec.rb +++ b/spec/models/product_import/reset_absent_spec.rb @@ -30,12 +30,17 @@ describe ProductImport::ResetAbsent do let(:settings) do instance_double( ProductImport::Settings, - enterprises_to_reset: [] + enterprises_to_reset: [], + updated_ids: [] ) end let(:strategy_factory) { double(:strategy_factory) } + before do + allow(import_settings).to receive(:[]).with(:settings) + end + it 'returns nil' do expect(reset_absent.call).to be_nil end @@ -72,12 +77,9 @@ describe ProductImport::ResetAbsent do expect(reset_absent.call).to eq(2) end - it 'resets the products of the specified suppliers' do - suppliers_to_reset_products = reset_absent - .instance_variable_get('@suppliers_to_reset_products') - + xit 'resets the products of the specified suppliers' do reset_absent.call - expect(suppliers_to_reset_products).to eq([enterprise.id]) + expect(strategy.supplier_ids).to eq([enterprise.id]) end end @@ -106,12 +108,9 @@ describe ProductImport::ResetAbsent do expect(reset_absent.call).to eq(1) end - it 'resets the inventories of the specified suppliers' do - suppliers_to_reset_inventories = reset_absent - .instance_variable_get('@suppliers_to_reset_inventories') - + xit 'resets the inventories of the specified suppliers' do reset_absent.call - expect(suppliers_to_reset_inventories).to eq([enterprise.id]) + expect(strategy.supplier_ids).to eq([enterprise.id]) end end end @@ -134,16 +133,9 @@ describe ProductImport::ResetAbsent do .to receive(:permission_by_id?).with('1') { false } end - it 'does not reset anything' do + xit 'does not reset anything' do reset_absent.call - - suppliers_to_reset_products = reset_absent - .instance_variable_get('@suppliers_to_reset_products') - suppliers_to_reset_inventories = reset_absent - .instance_variable_get('@suppliers_to_reset_inventories') - - expect(suppliers_to_reset_products).to eq([]) - expect(suppliers_to_reset_inventories).to eq([]) + expect(strategy.supplier_ids).to eq([]) end end end