Forked from munya/gist:a8d627da69196a859d95fb7fe6bd7c89
Created
June 29, 2016 05:32
-
-
Save johan--/040782c3643003fb628955ce59d8e323 to your computer and use it in GitHub Desktop.
CSV Data as stream
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 PropertiesController < ApplicationController | |
include ActionController::Live | |
before_action :find_county | |
def index | |
response.headers["Content-Type"] ||= 'text/csv' | |
response.headers["Content-Disposition"] = "attachment; filename=#{@county.csv_file_name}" | |
response.stream.write Property::CSV_HEADERS.to_csv | |
properties = @county.properties.processed | |
if params[:with_different_addresses_only] | |
properties = properties.where(has_different_addresses: true) | |
end | |
properties.find_in_batches(batch_size: 1000).each do |group| | |
group.each do |property| | |
row = [] | |
Property::CSV_COLUMN_NAME.each do |column| | |
row << property.send(column.to_sym).to_s | |
end | |
response.stream.write row.to_csv | |
end | |
end | |
response.stream.close | |
end | |
private | |
def find_county | |
@county = County.find(params[:county_id]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment