Sha256: f892da73622ca54ea52ad18a37775db0d3935628465373458736fd826f3b13dd

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 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 || '')
        end
      end

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

          collection.find_in_batches 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)

          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

5 entries across 5 versions & 1 rubygems

Version Path
effective_datatables-4.16.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.16.0 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.15.2 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.15.1 app/models/effective/effective_datatable/csv.rb
effective_datatables-4.15.0 app/models/effective/effective_datatable/csv.rb