Module: ActiveAdmin::ResourceController::Streaming

Included in:
ActiveAdmin::ResourceController
Defined in:
lib/active_admin/resource_controller/streaming.rb

Overview

This module overrides CSV responses to allow large data downloads. Could be expanded to JSON and XML in the future.

Instance Method Summary collapse

Instance Method Details

#csv_builderObject (protected)



32
33
34
# File 'lib/active_admin/resource_controller/streaming.rb', line 32

def csv_builder
  active_admin_config.csv_builder
end

#csv_filenameObject (protected)



36
37
38
# File 'lib/active_admin/resource_controller/streaming.rb', line 36

def csv_filename
  "#{resource_collection_name.to_s.gsub('_', '-')}-#{Time.zone.now.to_date.to_s}.csv"
end

#indexObject



11
12
13
14
15
16
# File 'lib/active_admin/resource_controller/streaming.rb', line 11

def index
  super do |format|
    format.csv { stream_csv }
    yield(format) if block_given?
  end
end

#stream_csvObject (protected)



40
41
42
43
44
# File 'lib/active_admin/resource_controller/streaming.rb', line 40

def stream_csv
  headers['Content-Type'] = 'text/csv; charset=utf-8' # In Rails 5 it's set to HTML??
  headers['Content-Disposition'] = %{attachment; filename="#{csv_filename}"}
  stream_resource &csv_builder.method(:build).to_proc.curry[self]
end

#stream_resource(&block) ⇒ Object (protected)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_admin/resource_controller/streaming.rb', line 20

def stream_resource(&block)
  headers['X-Accel-Buffering'] = 'no'
  headers['Cache-Control'] = 'no-cache'
  headers["Last-Modified"] = Time.current.httpdate

  if ActiveAdmin.application.disable_streaming_in.include? Rails.env
    self.response_body = block[String.new]
  else
    self.response_body = Enumerator.new &block
  end
end