Created
June 28, 2016 11:08
-
-
Save munya/a8d627da69196a859d95fb7fe6bd7c89 to your computer and use it in GitHub Desktop.
CSV Data as stream
This file contains hidden or 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