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.20.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.20.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.19.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.19.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.18.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.17.4 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.17.3 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.17.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.17.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.17.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.16.2 app/models/effective/effective_datatable/csv.rb