mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-31 21:37:16 +00:00
Extract Settings from Product Import processor
This encapsulates the data structure used by the entry processor to check various settings. It still requires a lot of work to move more logic to this new class.
This commit is contained in:
@@ -198,12 +198,14 @@ module ProductImport
|
||||
end
|
||||
|
||||
def assign_defaults(object, entry)
|
||||
settings = Settings.new(@import_settings)
|
||||
|
||||
# Assigns a default value for a specified field e.g. category='Vegetables', setting this value
|
||||
# either for all entries (overwrite_all), or only for those entries where the field was blank
|
||||
# in the spreadsheet (overwrite_empty), depending on selected import settings
|
||||
return unless @import_settings.key?(:settings) && @import_settings[:settings][entry.supplier_id.to_s] && @import_settings[:settings][entry.supplier_id.to_s]['defaults']
|
||||
return unless settings.defaults(entry)
|
||||
|
||||
@import_settings[:settings][entry.supplier_id.to_s]['defaults'].each do |attribute, setting|
|
||||
settings.defaults(entry).each do |attribute, setting|
|
||||
next unless setting['active']
|
||||
|
||||
case setting['mode']
|
||||
|
||||
13
app/models/product_import/settings.rb
Normal file
13
app/models/product_import/settings.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module ProductImport
|
||||
class Settings
|
||||
def initialize(import_settings)
|
||||
@import_settings = import_settings
|
||||
end
|
||||
|
||||
def defaults(entry)
|
||||
@import_settings.key?(:settings) &&
|
||||
@import_settings[:settings][entry.supplier_id.to_s] &&
|
||||
@import_settings[:settings][entry.supplier_id.to_s]['defaults']
|
||||
end
|
||||
end
|
||||
end
|
||||
51
spec/models/product_import/settings_spec.rb
Normal file
51
spec/models/product_import/settings_spec.rb
Normal file
@@ -0,0 +1,51 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ProductImport::Settings do
|
||||
let(:settings) { described_class.new(import_settings) }
|
||||
|
||||
describe '#defaults' do
|
||||
let(:entry) { instance_double(ProductImport::SpreadsheetEntry) }
|
||||
let(:import_settings) { {} }
|
||||
|
||||
context 'when there are no settings' do
|
||||
it 'returns false' do
|
||||
expect(settings.defaults(entry)).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
context 'when there are settings' do
|
||||
let(:entry) do
|
||||
instance_double(ProductImport::SpreadsheetEntry, supplier_id: 1)
|
||||
end
|
||||
let(:import_settings) { { settings: {} } }
|
||||
|
||||
context 'and there is no data for the specified entry' do
|
||||
it 'returns a falsey' do
|
||||
expect(settings.defaults(entry)).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
context 'and there is data for the specified entry' do
|
||||
context 'and it has no defaults' do
|
||||
let(:import_settings) do
|
||||
{ settings: { '1' => { 'foo' => 'bar' } } }
|
||||
end
|
||||
|
||||
it 'returns a falsey' do
|
||||
expect(settings.defaults(entry)).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
context 'and it has defaults' do
|
||||
let(:import_settings) do
|
||||
{ settings: { '1' => { 'defaults' => 'default value' } } }
|
||||
end
|
||||
|
||||
it 'returns a truthy' do
|
||||
expect(settings.defaults(entry)).to eq('default value')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user