Skip to content

Instantly share code, notes, and snippets.

@solars
Last active November 3, 2016 08:18
Show Gist options
  • Save solars/273512ae56397a445632687001b109aa to your computer and use it in GitHub Desktop.
Save solars/273512ae56397a445632687001b109aa to your computer and use it in GitHub Desktop.
def costs
startdate = Date.parse(params[:startdate])
enddate = Date.parse(params[:enddate])
respond_to do |format|
format.csv do
headers['X-Accel-Buffering'] = 'no' # Stop NGINX from buffering
headers["Cache-Control"] = "no-cache" # Stop downstream caching
headers["Transfer-Encoding"] = "chunked" # Chunked response header
headers.delete("Content-Length") # See one line above
headers["Content-Disposition"] = %(attachment; filename="export.csv")
self.response_body = Enumerator.new do |lines|
lines << ['date', 'property_id', 'channel_id', 'Amount EUR', 'Amount Hotel Currency', 'Hotel Currency'].join(',').to_s
ArCost.includes(:property).where(report_date: startdate..enddate).find_each do |cost|
lines << [cost.report_date, cost.property.property_id, cost.channel_id, cost.channel_cost_eur, cost.channel_cost_hotel_currency, cost.hotel_currency].join(',').to_s
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment