Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Commit

Permalink
Refactor default attribute processing for TARIC records.
Browse files Browse the repository at this point in the history
  • Loading branch information
saulius committed Apr 15, 2013
1 parent b20e790 commit 4058659
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 34 deletions.
27 changes: 15 additions & 12 deletions lib/tariff_importer/importers/taric_importer/record_processor.rb
Expand Up @@ -111,11 +111,20 @@ def record=(record)
end

def attributes=(attributes)
@attributes = if attribute_processor = attributes_for(klass)
attribute_processor.call(attributes)
else
attributes
end
attributes = if attribute_processor = attributes_for(klass)
attribute_processor.call(attributes)
else
attributes
end

@attributes = default_attributes.merge(attributes)
end

def default_attributes
klass.columns.inject({}) { |memo, column_name|
memo.merge!(Hash[column_name.to_s, nil])
memo
}
end

def operation=(operation)
Expand Down Expand Up @@ -154,13 +163,7 @@ def default_process
nil
when :update
model = klass.filter(attributes.slice(*primary_key).symbolize_keys).first
# TODO move to attributes=
defaults = model.columns.inject({}) { |memo, col|
memo.merge!(Hash[col.to_s, nil])
memo
}
record_attributes = defaults.merge(attributes)
model.set(record_attributes.except(*primary_key).symbolize_keys)
model.set(attributes.except(*primary_key).symbolize_keys)
model.save(validate: false)
model
end
Expand Down
Expand Up @@ -141,7 +141,9 @@
}

it 'returns attributes passed through override filter' do
processor.attributes.should eq ({ "description"=>"French" })
processor.attributes.should eq ({ "description"=>"French",
"language_code_id" => nil,
"language_id" => nil })
end
end

Expand Down Expand Up @@ -221,32 +223,59 @@
end

context 'update' do
let(:record) {
{"transaction.id"=>"31946",
"record.code"=>"130",
"subrecord.code"=>"05",
"record.sequence.number"=>"1",
"update.type"=>"1",
"language.description"=>
{"language.code.id"=>"FR",
"language.id"=>"EN",
"description"=>"French!"}}
}
context 'all values get updated' do
let(:record) {
{"transaction.id"=>"31946",
"record.code"=>"130",
"subrecord.code"=>"05",
"record.sequence.number"=>"1",
"update.type"=>"1",
"language.description"=>
{"language.code.id"=>"FR",
"language.id"=>"EN",
"description"=>"French!"}}
}

before {
create :language_description, language_code_id: 'FR',
language_id: 'EN',
description: 'French'
before {
create :language_description, language_code_id: 'FR',
language_id: 'EN',
description: 'French'

processor.process!
}
processor.process!
}

it 'does not create new records' do
LanguageDescription.count.should eq 1
it 'does not create new records' do
LanguageDescription.count.should eq 1
end

it 'updates the record' do
LanguageDescription.first.description.should eq 'French!'
end
end

it 'updates the record' do
LanguageDescription.first.description.should eq 'French!'
context 'update does not include all values' do
let(:record) {
{"transaction.id"=>"31946",
"record.code"=>"130",
"subrecord.code"=>"05",
"record.sequence.number"=>"1",
"update.type"=>"1",
"language.description"=>
{"language.code.id"=>"FR",
"language.id"=>"EN"}}
}

before {
create :language_description, language_code_id: 'FR',
language_id: 'EN',
description: 'English'

processor.process!
}

it 'sets missing update fields to nil' do
LanguageDescription.first.description.should eq nil
end
end
end

Expand Down

0 comments on commit 4058659

Please sign in to comment.