Skip to content

Commit

Permalink
Switched to send_stream for CSV download
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 16, 2024
1 parent 65a1634 commit 4869e57
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions app/controllers/blazer/queries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Blazer
class QueriesController < BaseController
include ActionController::Live

before_action :set_query, only: [:show, :edit, :update, :destroy, :refresh]
before_action :set_data_source, only: [:tables, :docs, :schema, :cancel]

Expand Down Expand Up @@ -271,9 +273,10 @@ def render_run
# not ideal, but useful for testing
raise Error, @error if @error && Rails.env.test?

data = csv_data(@columns, @rows, @data_source)
filename = "#{@query.try(:name).try(:parameterize).presence || 'query'}.csv"
send_data data, type: "text/csv; charset=utf-8", disposition: "attachment", filename: filename
send_stream type: "text/csv; charset=utf-8", disposition: "attachment", filename: filename do |stream|
csv_data(stream, @columns, @rows, @data_source)
end
end
end
end
Expand Down Expand Up @@ -369,12 +372,10 @@ def blazer_params
params[:blazer] || {}
end

def csv_data(columns, rows, data_source)
CSV.generate do |csv|
csv << columns
rows.each do |row|
csv << row.each_with_index.map { |v, i| v.is_a?(Time) ? blazer_time_value(data_source, columns[i], v) : v }
end
def csv_data(stream, columns, rows, data_source)
stream.write CSV.generate_line(columns)
rows.each do |row|
stream.write CSV.generate_line(row.each_with_index.map { |v, i| v.is_a?(Time) ? blazer_time_value(data_source, columns[i], v) : v })
end
end

Expand Down

0 comments on commit 4869e57

Please sign in to comment.