Sha256: a37908c9e219c9ed3576e468c8f823eae9679e889ee46aa44c2a30a72b891003

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
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[String.new]
        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}.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

8 entries across 8 versions & 1 rubygems

Version Path
activeadmin-3.3.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.2.5 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.2.4 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.2.3 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.2.2 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.2.1 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.2.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-3.1.0 lib/active_admin/resource_controller/streaming.rb