Sha256: 366954114b1867a7b57dfa34c5876e357de184275eb9d65ac27ae7b9dc68502c

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

require "csv"

module ActiveAdmin
  class ResourceController < BaseController

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

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

      protected

      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[""]
        else
          self.response_body = Enumerator.new &block
        end
      end

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

      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 &active_admin_config.csv_builder.method(:build).to_proc.curry[self]
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/resource_controller/streaming.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activeadmin-2.9.0/lib/active_admin/resource_controller/streaming.rb
activeadmin-2.9.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.8.1 lib/active_admin/resource_controller/streaming.rb