Module: Effective::EffectiveDatatable::Csv

Included in:
Datatable
Defined in:
app/models/effective/effective_datatable/csv.rb

Instance Method Summary collapse

Instance Method Details

#csv_collectionObject



54
55
56
57
58
59
60
# File 'app/models/effective/effective_datatable/csv.rb', line 54

def csv_collection
  value = if active_record_collection? && @_collection_apply_scope
    column_tool.scope(collection)
  else
    collection
  end
end

#csv_content_typeObject



13
14
15
# File 'app/models/effective/effective_datatable/csv.rb', line 13

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

#csv_fileObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/effective/effective_datatable/csv.rb', line 31

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

#csv_filenameObject



9
10
11
# File 'app/models/effective/effective_datatable/csv.rb', line 9

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

#csv_headerObject



17
18
19
20
21
# File 'app/models/effective/effective_datatable/csv.rb', line 17

def csv_header
  columns.map do |name, opts|
    opts[:label].presence || csv_human_attribute_name(name)
  end
end

#csv_human_attribute_name(name) ⇒ Object



23
24
25
26
27
28
29
# File 'app/models/effective/effective_datatable/csv.rb', line 23

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

#csv_streamObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/effective/effective_datatable/csv.rb', line 62

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

    if active_record_collection?
      csv_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 = csv_collection()

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

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

  end
end