Sha256: 6a0699de475c70056721a8789143c8c40d43231dfa4e148ba8b8ac24df483063

Contents?: true

Size: 1.61 KB

Versions: 54

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require "csv"

module Decidim
  module Exporters
    # 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`.
    class CSV < Exporter
      # Public: Exports a CSV serialized version of the collection using the
      # provided serializer and following the previously described strategy.
      #
      # Returns an ExportData instance.
      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

      private

      def headers
        return [] if processed_collection.empty?
        processed_collection.first.keys
      end

      def processed_collection
        @processed_collection ||= collection.map do |resource|
          flatten(@serializer.new(resource).serialize)
        end
      end

      def flatten(object, key = nil)
        if object.is_a? Hash
          object.inject({}) do |result, (subkey, value)|
            new_key = key ? "#{key}/#{subkey}" : subkey.to_s
            result.merge(flatten(value, new_key))
          end
        elsif object.is_a?(Array)
          { key.to_s => object.map(&:to_s).join(", ") }
        else
          { key.to_s => object }
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
decidim-core-0.7.2 lib/decidim/exporters/csv.rb
decidim-core-0.7.1 lib/decidim/exporters/csv.rb
decidim-core-0.7.0 lib/decidim/exporters/csv.rb
decidim-core-0.6.8 lib/decidim/exporters/csv.rb
decidim-0.6.8 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.7 lib/decidim/exporters/csv.rb
decidim-0.6.7 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.6 lib/decidim/exporters/csv.rb
decidim-0.6.6 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.5 lib/decidim/exporters/csv.rb
decidim-0.6.5 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.4 lib/decidim/exporters/csv.rb
decidim-0.6.4 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.3 lib/decidim/exporters/csv.rb
decidim-0.6.3 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.2 lib/decidim/exporters/csv.rb
decidim-0.6.2 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.1 lib/decidim/exporters/csv.rb
decidim-0.6.1 decidim-core/lib/decidim/exporters/csv.rb
decidim-core-0.6.0 lib/decidim/exporters/csv.rb