-
Notifications
You must be signed in to change notification settings - Fork 1
/
exports_controller.rb
61 lines (52 loc) · 1.55 KB
/
exports_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'csv'
class ExportsController < ApplicationController
def index
end
def normal_csv
respond_to do |format|
format.csv do
csv = generate_csv(User.column_names, User.all)
send_data(csv)
end
end
end
def stream_csv
respond_to do |format|
format.csv do
csv_enumerator = generate_streaming_csv(User.column_names, User.all)
render_streaming_csv(csv_enumerator)
end
end
end
private
def generate_csv(column_names, records)
CSV.generate do |csv|
csv << column_names # add headers to the CSV
records.each do |record|
csv << record.attributes.values_at(*column_names)
end
end
end
def generate_streaming_csv(column_names, records)
Enumerator.new do |csv|
csv << column_names.to_csv # add headers to the CSV
records.each do |record|
csv << record.attributes.values_at(*column_names).to_csv
end
end
end
def render_streaming_csv(csv_enumerator)
# Delete this header so that Rack knows to stream the content.
headers.delete("Content-Length")
# Do not cache results from this action.
headers["Cache-Control"] = "no-cache"
# Let the browser know that this file is a CSV.
headers['Content-Type'] = 'text/csv'
# Do not buffer the result when using proxy servers.
headers['X-Accel-Buffering'] = 'no'
# Set the filename
headers['Content-Disposition'] = "attachment; filename=\"report.csv\""
# Set the response body as the enumerator
self.response_body = csv_enumerator
end
end