Sha256: ac7836a2ee698b29cd9a223b6443e9b5804141daec7e5005274d25f400ba6a8d

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'csv'

module Effective
  module EffectiveDatatable
    module Csv

      def csv_filename
        self.class.name.underscore.parameterize + '.csv'
      end

      def csv_content_type
        'text/csv; charset=utf-8'
      end

      def csv_header
        columns.map { |_, opts| opts[:label] || '' }
      end

      def csv_file
        CSV.generate do |csv|
          csv << csv_header()

          collection.send(csv_collection_method) do |resources|
            resources = arrayize(resources, csv: true)
            format(resources, csv: true)
            finalize(resources)

            resources.each { |resource| csv << resource }
          end
        end
      end

      def csv_stream
        EffectiveResources.with_resource_enumerator do |lines|
          lines << CSV.generate_line(csv_header)

          collection.public_send(csv_collection_method) do |resources|
            resources = arrayize(resources, csv: true)
            format(resources, csv: true)
            finalize(resources)

            resources.each { |resource| lines << CSV.generate_line(resource) }
          end
        end
      end

      private

      def csv_collection_method
        (active_record_collection? ? :find_in_batches : :to_a)
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
effective_datatables-4.13.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.13.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.13.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.12.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.11.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.11.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.11.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.7 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.6 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.5 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.4 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.3 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.10.1 app/models/effective/effective_datatable/csv.rb