Class: AwesomeImports::CsvImport

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::AttributeMethods, ActiveModel::Conversion
Defined in:
lib/awesome_imports/csv_import.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr = {}) ⇒ CsvImport

Returns a new instance of CsvImport.



23
24
25
26
# File 'lib/awesome_imports/csv_import.rb', line 23

def initialize(attr = {})
  attribute = attr.with_indifferent_access
  self.csv_file = attribute[:csv_file]
end

Instance Attribute Details

#csv_fileObject

Returns the value of attribute csv_file.



21
22
23
# File 'lib/awesome_imports/csv_import.rb', line 21

def csv_file
  @csv_file
end

#stored_csv_pathObject (readonly)

Returns the value of attribute stored_csv_path.



74
75
76
# File 'lib/awesome_imports/csv_import.rb', line 74

def stored_csv_path
  @stored_csv_path
end

#userObject

Returns the value of attribute user.



21
22
23
# File 'lib/awesome_imports/csv_import.rb', line 21

def user
  @user
end

Class Method Details

.restore_from_file(file_path) ⇒ Object



65
66
67
68
69
# File 'lib/awesome_imports/csv_import.rb', line 65

def self.restore_from_file(file_path)
  res = new
  res.load_stored_csv(file_path)
  res
end

Instance Method Details

#confirmObject



28
29
30
31
32
# File 'lib/awesome_imports/csv_import.rb', line 28

def confirm
  @records = parse_csv_from_uploaded_file
  records.each{ |record| record.valid? }
  records
end

#csv_tmp_dirObject



59
60
61
62
63
# File 'lib/awesome_imports/csv_import.rb', line 59

def csv_tmp_dir
  dir = File.join(Rails.root, "tmp", "csv_import")
  FileUtils.mkdir_p(dir)
  dir
end

#has_error?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/awesome_imports/csv_import.rb', line 95

def has_error?
  @records.any?{ |record| !record.errors.empty? }
end

#idObject



17
18
# File 'lib/awesome_imports/csv_import.rb', line 17

def id
end

#load_stored_csv(file_path) ⇒ Object



71
72
73
# File 'lib/awesome_imports/csv_import.rb', line 71

def load_stored_csv(file_path)
  @stored_csv_path = file_path
end

#parse_csv(file_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/awesome_imports/csv_import.rb', line 38

def parse_csv(file_path)
  rows = []
  CSV.foreach(file_path, :encoding => "utf-8") do |row|
    if respond_to?(:parse_row)
      rows << parse_row(row)
    else
      rows << row
    end
  end
  rows
end

#parse_csv_from_stored_fileObject



76
77
78
# File 'lib/awesome_imports/csv_import.rb', line 76

def parse_csv_from_stored_file
  parse_csv(stored_csv_path)
end

#parse_csv_from_uploaded_fileObject



34
35
36
# File 'lib/awesome_imports/csv_import.rb', line 34

def parse_csv_from_uploaded_file
  parse_csv(csv_file.tempfile)
end

#persisted?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/awesome_imports/csv_import.rb', line 13

def persisted?
  csv_file.present? or stored_csv_path.present?
end

#recordsObject



50
51
52
# File 'lib/awesome_imports/csv_import.rb', line 50

def records
  @records || []
end

#store_attached_csv_fileObject



54
55
56
57
# File 'lib/awesome_imports/csv_import.rb', line 54

def store_attached_csv_file
  FileUtils.cp(csv_file.tempfile.path, csv_tmp_dir)
  File.join(csv_tmp_dir, File.basename(csv_file.tempfile.path))
end

#updateObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/awesome_imports/csv_import.rb', line 80

def update
  @records = parse_csv_from_stored_file
  ActiveRecord::Base.transaction do
    records.each do |record|
      record.save!
    end
  end
  FileUtils.rm(stored_csv_path)
  remove_old_stored_csvs
  true
rescue ActiveRecord::RecordInvalid => e
  records.each{ |record| record.valid? }
  false
end