Class: Decidim::Admin::Import::Readers::CSV

Inherits:
Base
  • Object
show all
Defined in:
lib/decidim/admin/import/readers/csv.rb

Overview

Imports any exported CSV file to local objects. It transforms the import data using the creator into the final target objects.

Constant Summary collapse

MIME_TYPE =
"text/csv"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Decidim::Admin::Import::Readers::Base

Class Method Details

.first_data_indexObject



14
15
16
# File 'lib/decidim/admin/import/readers/csv.rb', line 14

def self.first_data_index
  1
end

Instance Method Details

#example_file(data) ⇒ Object

Returns a StringIO



25
26
27
28
29
30
31
32
33
# File 'lib/decidim/admin/import/readers/csv.rb', line 25

def example_file(data)
  csv_data = ::CSV.generate(col_sep: Decidim.default_csv_col_sep) do |csv|
    data.each do |row|
      csv << row
    end
  end

  ::StringIO.new(csv_data)
end

#read_rowsObject



18
19
20
21
22
# File 'lib/decidim/admin/import/readers/csv.rb', line 18

def read_rows
  ::CSV.read(file, col_sep: Decidim.default_csv_col_sep, encoding: "BOM|UTF-8").each_with_index do |row, index|
    yield row, index
  end
end