Created
February 24, 2017 15:35
-
-
Save spencerldixon/40e42c392a0ccccc122d7787631b0897 to your computer and use it in GitHub Desktop.
Simple CSV Exporter Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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