class FatFreeCRM::ExportCSV

Public Class Methods

from_array(items = []) click to toggle source

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

# 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