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`.

Direct Known Subclasses

Excel

Instance Method Summary collapse

Methods inherited from Exporter

#initialize

Constructor Details

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

Instance Method Details

#export(col_sep = Decidim.default_csv_col_sep) ⇒ Object

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
# File 'lib/decidim/exporters/csv.rb', line 18

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

#headersObject



27
28
29
30
31
# File 'lib/decidim/exporters/csv.rb', line 27

def headers
  return [] if processed_collection.empty?

  @headers ||= processed_collection.inject([]) { |keys, resource| keys | resource.keys }
end