Module: ActsAsExportable::Exporter::ClassMethods

Defined in:
lib/acts_as_exportable/exporter.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_exportable(*args) ⇒ Object

Make a model a exportable. This allows a Class to export all its data.

Example:

class Contact < ActiveRecord::Base
  acts_as_exportable
end


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/acts_as_exportable/exporter.rb', line 16

def acts_as_exportable(*args)

  cattr_accessor :csv_library

  if RUBY_VERSION >= "1.9"
    self.csv_library = CSV
  else
    self.csv_library = FasterCSV
  end

  cattr_accessor :exported_attrs
  self.exported_attrs = args
end

#build_csvObject

Find all the records and build a CSV string

Contact.build_csv



34
35
36
37
38
39
40
41
42
# File 'lib/acts_as_exportable/exporter.rb', line 34

def build_csv
  list = self.all(:order => "created_at DESC")
  csv_string = csv_library.generate do |csv|
    csv << included_columns
    list.each do |item|
      csv << included_columns.map{ |a| item[a.to_sym]}
    end
  end
end

#included_columnsObject

Determine whether to include all attributes or just those specified.



48
49
50
51
52
53
54
# File 'lib/acts_as_exportable/exporter.rb', line 48

def included_columns
  if exported_attrs.blank?
    self.column_names
  else
    exported_attrs
  end
end