Sha256: a5a52601463b3848854656065c2e521d1f67dfa0f0b6715b99867dde24b8fc2c

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
require "csv"

module FatFreeCRM
  class ExportCSV
    # CSV export. Based on to_csv Rails plugin by Ary Djmal
    # https://github.com/arydjmal/to_csv
    #----------------------------------------------------------------------------
    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fat_free_crm-0.22.1 lib/fat_free_crm/export_csv.rb
fat_free_crm-0.22.0 lib/fat_free_crm/export_csv.rb
fat_free_crm-0.21.0 lib/fat_free_crm/export_csv.rb
fat_free_crm-0.20.1 lib/fat_free_crm/export_csv.rb
fat_free_crm-0.20.0 lib/fat_free_crm/export_csv.rb
fat_free_crm-0.19.2 lib/fat_free_crm/export_csv.rb
fat_free_crm-0.19.0 lib/fat_free_crm/export_csv.rb