Module: Export::Download

Defined in:
lib/export/download.rb

Overview

Code that translates scopes into downloadable tab-delimited CSV. Dependant on Postgresql.

Class Method Summary collapse

Class Method Details

.delete_columns(tbl, columns = []) ⇒ CSV::Table

Returns delete the specified columns.

Parameters:

  • table (CSV::Table)
  • columns (Array) (defaults to: [])

Returns:

  • (CSV::Table)

    delete the specified columns



59
60
61
62
63
64
65
# File 'lib/export/download.rb', line 59

def self.delete_columns(tbl, columns = [])
  return tbl if columns.empty?
  columns.each do |col|
    tbl.delete(col.to_s)
  end
  tbl
end

.generate_csv(scope, exclude_columns: [], header_converters: [], trim_rows: false, trim_columns: false) ⇒ CSV

translate a scope into a CSV table, with optional tweaks to the data

This is a very nice reference for future consideration:

http://collectiveidea.com/blog/archives/2015/03/05/optimizing-rails-for-memory-usage-part-3-pluck-and-database-laziness

Parameters:

  • scope (Scope)
  • exclude_columns (Array) (defaults to: [])
  • header_converters (Array) (defaults to: [])
  • trim_rows (Boolean) (defaults to: false)
  • trim_columns (Boolean) (defaults to: false)

Returns:

  • (CSV)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/export/download.rb', line 15

def self.generate_csv(scope, exclude_columns: [], header_converters: [], trim_rows: false, trim_columns: false)

  # Check to see if keys is deterministicly ordered
  column_names = scope.columns_hash.keys

  h = CSV.new(column_names.join(','), header_converters: header_converters, headers: true)
  h.read

  headers = CSV::Row.new(h.headers, h.headers, true)

  tbl = CSV::Table.new([headers])

  # Pluck rows is from postgresql_cursor gem
  scope.pluck_rows(*column_names).each do |o|
    tbl << o.collect{|v| Utilities::Strings.sanitize_for_csv(v) }
    # If keys are not deterministic: .attributes.values_at(*column_names).collect{|v| Utilities::Strings.sanitize_for_csv(v) }
  end

  if !exclude_columns.empty?
    Rainbow('deleting columns: ' + Benchmark.measure {
      delete_columns(tbl, exclude_columns)
    }.to_s).yellow
  end

  if trim_columns
    Rainbow('trimming columns: ' + Benchmark.measure {
      trim_columns(tbl)
    }.to_s).yellow
  end

  if trim_rows 
    Rainbow('trimming rows: ' + Benchmark.measure {
      trim_rows(tbl)
    }.to_s).yellow
  end

  # CSV::Converters are only available on read, not generate, so we can't use them here.
  tbl.to_csv(col_sep: "\t", encoding: Encoding::UTF_8)
end

.trim_columns(table) ⇒ CSV::Table

Parameters:

  • table (CSV::Table)

    remove columns without any non-#blank? values (of course doing this in the scope is better!) this is very slow, use a proper scope, and exclude_columns: [] options instead

Returns:

  • (CSV::Table)


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/export/download.rb', line 90

def self.trim_columns(table)
  table.by_col!.delete_if do |h, col|
    d = true
    col.shift
    col.each do |v|
      next if v.blank?
      d = false
      break
    end
    d
  end
  table
end

.trim_rows(table, skip_id = true) ⇒ CSV::Table

Parameters:

  • table (CSV::Table)
  • skip_id (Boolean) (defaults to: true)

    if true ignore the 'id'l column in consideration of whether to delete this row delete rows if there are no values in any cell (of course doing this in the scope is better!)

Returns:

  • (CSV::Table)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/export/download.rb', line 71

def self.trim_rows(table, skip_id = true)
  headers = table.headers
  headers = headers - ['id'] if skip_id
  table.by_row!.delete_if do |row|
    d = true
    headers.each do |h|
      next if row[h].blank?
      d = false
      break
    end
    d
  end
  table
end