Skip to content

Instantly share code, notes, and snippets.

@spencerldixon
Created February 24, 2017 15:35
Show Gist options
  • Save spencerldixon/40e42c392a0ccccc122d7787631b0897 to your computer and use it in GitHub Desktop.
Save spencerldixon/40e42c392a0ccccc122d7787631b0897 to your computer and use it in GitHub Desktop.
Simple CSV Exporter Module
module CsvExporter
def self.export_results(results)
folder = Rails.root.join("public", "csv_exports")
FileUtils.mkdir_p(folder)
filepath = folder.join("csv_export_#{DateTime.now.to_i}.csv")
CSV.open(filepath, "wb") do |csv|
csv << self.headers
results.each do |result|
csv << self.format_row(result)
end
end
filepath
end
private
def self.headers
[
"ID",
"Created at"
]
end
def self.format_row(result)
[
result.id,
result.created_at
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment