Class: RailsAdminImport::Formats::CSVImporter

Inherits:
FileImporter
  • Object
show all
Defined in:
lib/rails_admin_import/formats/csv_importer.rb

Instance Attribute Summary

Attributes inherited from FileImporter

#error, #filename, #import_model

Instance Method Summary collapse

Methods inherited from FileImporter

#each, #valid?

Constructor Details

#initialize(import_model, params) ⇒ CSVImporter

Returns a new instance of CSVImporter.



12
13
14
15
16
# File 'lib/rails_admin_import/formats/csv_importer.rb', line 12

def initialize(import_model, params)
  super
  @encoding = params[:encoding]
  @header_converter = RailsAdminImport.config.header_converter
end

Instance Method Details

#each_recordObject

A method that yields a hash of attributes for each record to import



19
20
21
22
23
24
# File 'lib/rails_admin_import/formats/csv_importer.rb', line 19

def each_record
  CSV.foreach(filename, **csv_options) do |row|
    attr = convert_to_attributes(row)
    yield attr unless attr.all? { |field, value| value.blank? }
  end
end