Module: Import

Defined in:
lib/awesome_gem.rb

Class Method Summary collapse

Class Method Details

.import(file, model_name, columns) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/awesome_gem.rb', line 3

def import(file,model_name,columns)
  case File.extname(file.original_filename)
  when ".csv" then self.import_csv(file,model_name)
  when ".xls" then self.import_xls(file,model_name,columns)
  when ".xlsx" then self.import_xlsx(file,model_name,columns)
  else return
  end
end

.import_csv(file, model_name) ⇒ Object



19
20
21
22
23
# File 'lib/awesome_gem.rb', line 19

def import_csv(file,model_name)
  CSV.foreach(file.path, headers: true) do |row|
    model_name.create! row.to_hash 
  end
end

.import_xls(file, model_name, columns) ⇒ Object



12
13
14
15
16
17
# File 'lib/awesome_gem.rb', line 12

def import_xls(file,model_name,columns)
  Roo::Excel.new(file.path).each_with_index(columns) do |a,index|
    next if index.eql?(0)
    model_name.create! a
  end
end

.import_xlsx(file, model_name, columns) ⇒ Object



24
25
26
27
28
29
# File 'lib/awesome_gem.rb', line 24

def import_xlsx(file,model_name,columns)
  Roo::Excelx.new(file.path).each_with_index(columns) do |a,index|
    next if index.eql?(0)
    model_name.create! a
  end
end