9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/jpstation/csv/importable.rb', line 9
def import(file)
current_ids = []
CSV.foreach(file, headers: true, converters: :numeric, header_converters: :symbol) do |row|
if row[:e_status] == 0 || paranoid?
model = unscoped.find_or_initialize_by(id: row.first)
model.assign_attributes(.inject({}) { |attr, (k, v)| attr[k] = row[v]; attr })
model.deleted_at = (row[:e_status] == 0) ? nil : Time.now if paranoid?
model.save!
current_ids << model.id
end
end
unscoped.where.not(id: current_ids).destroy_all
end
|