Skip to content

Commit

Permalink
Add update task.
Browse files Browse the repository at this point in the history
dur: 20min.
  • Loading branch information
hanshasselberg committed Jun 28, 2012
1 parent 0e83a5e commit b27d683
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/tasks/csv_import.rake
@@ -1,5 +1,4 @@
namespace :csv do

desc 'Import csv'
task :import => :environment do
File.open("doc/datenbank-digital-hans_new.csv", "r").lines do |line|
Expand All @@ -12,4 +11,27 @@ namespace :csv do
end
end

desc "Update csv"
task :update => :environment do
updated_devices = []

# update/create
File.open("doc/datenbank_update_19_06_2012_inventar.csv", "r").lines do |line|
barcode, serial, model, company, kind = line.split(",")[0,5].map(&:strip)
next if kind == "Art" || kind == ""
barcode = "%06d" % barcode.to_i
device = Device.where(barcode: barcode).first
if device
device.update_attributes(model: model, company: company, kind: kind)
else
device = Device.create(
barcode: barcode, model: model, company: company, kind: kind
)
end
updated_devices << device
end

# delete
(Device.all.entries - updated_devices).map(&:destroy)
end
end

0 comments on commit b27d683

Please sign in to comment.