Class: FatFreeCRM::ExportCSV

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_free_crm/export_csv.rb

Class Method Summary collapse

Class Method Details

.from_array(items = []) ⇒ Object

CSV export. Based on to_csv Rails plugin by Ary Djmal github.com/arydjmal/to_csv




15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fat_free_crm/export_csv.rb', line 15

def self.from_array(items = [])
  return '' if items.empty?
  # Infer column types from the first item in the array
  klass = items.first.class
  columns = klass.columns.map(&:name).reject { |column| column =~ /password|token/ }
  columns << 'tags' if klass.taggable?
  CSV.generate do |csv|
    csv << columns.map { |column| klass.human_attribute_name(column) }
    items.each do |item|
      csv << columns.map do |column|
        if column == 'tags'
          item.tags.join(' ')
        else
          item.send(column)
        end
      end
    end
  end
end