Sha256: e36a0636ff20961d4230d7c02b9bed92d7a8f913076ae4fea51ca2ad203db1e8

Contents?: true

Size: 1.29 KB

Versions: 14

Compression:

Stored size: 1.29 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] # rubocop:disable Performance/UnfreezeString to preserve encoding
        else
          self.response_body = Enumerator.new &block
        end
      end

      def csv_filename
        "#{resource_collection_name.to_s.tr('_', '-')}-#{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

14 entries across 14 versions & 1 rubygems

Version Path
activeadmin-4.0.0.beta14 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta13 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta12 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta11 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta10 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta9 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta8 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta7 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta6 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta5 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta4 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta3 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta2 app/controllers/active_admin/resource_controller/streaming.rb
activeadmin-4.0.0.beta1 app/controllers/active_admin/resource_controller/streaming.rb