Don't use importing_into_inventory? in ResetAbsent

This completely decouples ResetAbsent from the particular strategy used.
This commit is contained in:
Pau Perez
2018-09-25 18:31:25 +02:00
parent a10e58e20a
commit 54d6bc5443
5 changed files with 44 additions and 57 deletions

View File

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

View File

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

View File

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

View File

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

View File

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