Inject #updated_ids to strategy

Their values are known beforehand.
This commit is contained in:
Pau Perez
2018-09-25 21:43:08 +02:00
parent 186801a1e2
commit 5eb10edbfd
5 changed files with 13 additions and 17 deletions

View File

@@ -81,7 +81,7 @@ module ProductImport
end
def strategy
@strategy ||= strategy_factory.new
@strategy ||= strategy_factory.new(settings.updated_ids)
end
def total_saved_count

View File

@@ -2,16 +2,16 @@ module ProductImport
class InventoryReset
attr_reader :supplier_ids
def initialize
def initialize(updated_ids)
@supplier_ids = []
@updated_ids = updated_ids
end
def <<(values)
@supplier_ids << values
end
def reset(updated_ids)
@updated_ids = updated_ids
def reset
relation.update_all(count_on_hand: 0)
end

View File

@@ -2,16 +2,16 @@ module ProductImport
class ProductsReset
attr_reader :supplier_ids
def initialize
def initialize(updated_ids)
@supplier_ids = []
@updated_ids = updated_ids
end
def <<(values)
@supplier_ids << values
end
def reset(updated_ids)
@updated_ids = updated_ids
def reset
relation.update_all(count_on_hand: 0)
end

View File

@@ -27,7 +27,7 @@ module ProductImport
attr_reader :settings, :strategy, :entry_processor
def reset_stock
@products_reset_count += strategy.reset(settings.updated_ids)
@products_reset_count += strategy.reset
end
end
end

View File

@@ -13,8 +13,7 @@ module ProductImport
let(:settings) do
instance_double(
Settings,
enterprises_to_reset: [],
updated_ids: []
enterprises_to_reset: []
)
end
@@ -27,13 +26,12 @@ module ProductImport
end
end
context 'when there are updated_ids and enterprises_to_reset' do
context 'when there are enterprises_to_reset' do
let(:enterprise) { instance_double(Enterprise, id: 1) }
let(:settings) do
instance_double(
Settings,
updated_ids: [0],
enterprises_to_reset: [enterprise.id.to_s]
)
end
@@ -46,7 +44,7 @@ module ProductImport
allow(strategy).to receive(:<<).with(enterprise.id)
allow(strategy).to receive(:supplier_ids) { [enterprise.id] }
allow(strategy).to receive(:reset).with([0]) { 2 }
allow(strategy).to receive(:reset) { 2 }
end
it 'returns the number of products reset' do
@@ -54,7 +52,7 @@ module ProductImport
end
it 'resets the products of the specified suppliers' do
expect(strategy).to receive(:reset).with([0]) { 2 }
expect(strategy).to receive(:reset) { 2 }
reset_absent.call
end
end
@@ -65,7 +63,6 @@ module ProductImport
let(:settings) do
instance_double(
Settings,
updated_ids: [0],
enterprises_to_reset: [enterprise.id.to_s]
)
end
@@ -92,7 +89,6 @@ module ProductImport
let(:settings) do
instance_double(
Settings,
updated_ids: [0],
enterprises_to_reset: [enterprise.id.to_s]
)
end
@@ -105,7 +101,7 @@ module ProductImport
allow(strategy).to receive(:<<).with(enterprise.id)
allow(strategy).to receive(:supplier_ids) { [enterprise.id] }
allow(strategy).to receive(:reset).with([0]) { 1 }
allow(strategy).to receive(:reset) { 1 }
end
it 'returns the number of reset variant overrides' do