Class: RailsAdminImport::Formats::CSVImporter

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

Constant Summary collapse

HEADER_CONVERTER =

Default is to downcase headers and add underscores to convert into attribute names

lambda do |header|
  header.parameterize.underscore
end

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.



14
15
16
17
18
# File 'lib/rails_admin_import/formats/csv_importer.rb', line 14

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

Instance Method Details

#each_recordObject

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



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

def each_record
  CSV.foreach(filename, csv_options) do |row|
    yield convert_to_attributes(row)
  end
end