Sha256: 8595d9ea8a3ddea71379621d409e5d4955cbb89513d4e0a12139200d77ca7997

Contents?: true

Size: 1.91 KB

Versions: 31

Compression:

Stored size: 1.91 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 do |name, opts|
          opts[:label].presence || csv_human_attribute_name(name)
        end
      end

      def csv_human_attribute_name(name)
        if active_record_collection?
          collection_class.human_attribute_name(name)
        else
          (name.to_s.split('.').last.titleize|| '')
        end
      end

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

          if active_record_collection?
            collection.find_in_batches do |resources|
              resources = arrayize(resources, csv: true)
              format(resources, csv: true)
              finalize(resources)

              resources.each { |resource| csv << resource }
            end
          else
            resources = collection

            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)

          if active_record_collection?
            collection.find_in_batches do |resources|
              resources = arrayize(resources, csv: true)
              format(resources, csv: true)
              finalize(resources)

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

            format(resources, csv: true)
            finalize(resources)

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

        end
      end

    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
effective_datatables-4.26.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.25.3 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.25.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.25.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.25.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.24.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.24.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.24.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.23.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.22.3 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.22.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.22.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.22.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.21.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.21.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.20.6 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.20.5 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.20.4 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.20.3 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.20.2 app/models/effective/effective_datatable/csv.rb