module ActsAsExportable::Exporter::ClassMethods

Public Instance Methods

acts_as_exportable(*args) click to toggle source

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

Example:

class Contact < ActiveRecord::Base
  acts_as_exportable
end
# 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_csv() click to toggle source

Find all the records and build a CSV string

Contact.build_csv

# 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_columns() click to toggle source

Determine whether to include all attributes or just those specified.

# File lib/acts_as_exportable/exporter.rb, line 48
def included_columns
  if exported_attrs.blank?
    self.column_names
  else
    exported_attrs
  end
end