Class: Decidim::Exporters::CSV

Inherits:
Exporter
  • Object
show all
Defined in:
lib/decidim/exporters/csv.rb

Overview

Exports any serialized object (Hash) into a readable CSV. It transforms the columns using slashes in a way that can be afterwards reconstructed into the original nested hash.

For example, ‘{ name: { ca: “Hola”, en: “Hello” } }` would result into the columns: `name/ca` and `name/es`.

Instance Method Summary collapse

Methods inherited from Exporter

#initialize

Constructor Details

This class inherits a constructor from Decidim::Exporters::Exporter

Instance Method Details

#exportObject

Public: Exports a CSV serialized version of the collection using the provided serializer and following the previously described strategy.

Returns an ExportData instance.



18
19
20
21
22
23
24
25
26
# File 'lib/decidim/exporters/csv.rb', line 18

def export
  data = ::CSV.generate(headers: headers, write_headers: true, col_sep: ";") do |csv|
    processed_collection.each do |resource|
      csv << headers.map { |header| resource[header] }
    end
  end

  ExportData.new(data, "csv")
end