Created
October 9, 2013 17:29
-
-
Save samnang/6905002 to your computer and use it in GitHub Desktop.
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
class ReportsController < ApplicationController | |
before_filter :authenticate_csm! | |
def pending | |
#send_csv_data(PendingCasesReport.new.csv, 'pending_cases.csv') | |
setup_streaming_headers | |
self.response_body = streamer | |
end | |
def index | |
end | |
private | |
def setup_streaming_headers | |
file_name = "report.csv" | |
headers["Content-Type"] = "text/csv" | |
headers["Content-disposition"] = "attachment; filename=\"#{file_name}\"" | |
headers["Cache-Control"] ||= "no-cache" | |
headers["Transfer-Encoding"] = "chunked" | |
end | |
def streamer | |
connection = CaseType.connection | |
connection.execute "COPY (#{CaseType.scoped.to_sql}) TO STDOUT WITH CSV HEADER" | |
stream_enum = Enumerator.new do |y| | |
while line = connection.raw_connection.get_copy_data do | |
y << line | |
sleep(1) | |
end | |
end | |
Rack::Chunked::Body.new(stream_enum) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment