Class: Import
- Inherits:
-
Object
- Object
- Import
- Defined in:
- app/models/import.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#initialize(klass, file) ⇒ Import
constructor
A new instance of Import.
- #perform! ⇒ Object
Constructor Details
#initialize(klass, file) ⇒ Import
Returns a new instance of Import.
6 7 8 9 10 11 12 13 14 |
# File 'app/models/import.rb', line 6 def initialize(klass, file) @klass = klass @file = file validate_import # TODO: initialize import job instead of doing import here perform! end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
4 5 6 |
# File 'app/models/import.rb', line 4 def file @file end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
4 5 6 |
# File 'app/models/import.rb', line 4 def klass @klass end |
Instance Method Details
#perform! ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/models/import.rb', line 16 def perform! # TODO: this creates a new object per CSV row. It may be worth optimizing further to use # something like this approach to do only a single insert. # https://gist.github.com/jackrg/76ade1724bd816292e4e CSV.foreach(file.tempfile, headers: true, encoding:'iso-8859-1:utf-8', skip_blanks: true, skip_lines: /^(?:,\s*)+$/, header_converters: -> (f) { f.strip }, converters: -> (f) { f ? f.strip : nil } ) do |row| attrs = row.to_h.compact klass.find_or_create_by!(attrs) end end |