Module: DataImportHelper

Included in:
TableList
Defined in:
app/models/concerns/data_import_helper.rb

Instance Method Summary collapse

Instance Method Details

#import_columnsObject



13
14
15
16
17
18
19
20
# File 'app/models/concerns/data_import_helper.rb', line 13

def import_columns
  config = data_list.config_table
  columns = {}
  config.columns.each do |key, value|
    columns[key] = config.columns[key].merge(index: self.headers.find_index(value[:header]))
  end
  columns.reject { |_, v| v[:index].nil? }
end

#import_to_table_list(file) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'app/models/concerns/data_import_helper.rb', line 3

def import_to_table_list(file)
  importer = data_list.importer(file)
  self.headers = importer.results[0]
  self.done = true
  self.save
  importer.results[1..-1].each do |row|
    table_items.create(fields: row)
  end
end

#migrateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/concerns/data_import_helper.rb', line 22

def migrate
  config = data_list.config_table
  columns = import_columns
  self.table_items.each do |table_item|
    attr = {}
    columns.map do |key, value|
      r = table_item.fields[value[:index]]
      if value[:field] && value[:field].respond_to?(:call)
        attr[key] = value[:field].call(r)
      else
        attr[key] = r
      end
    end
    config.record.create attr
  end
  self.destroy
end