Sha256: cfe8485b21de0c9447cdb173150f9283f299f1d2343659e3435f1d4cde8426dc

Contents?: true

Size: 1.15 KB

Versions: 40

Compression:

Stored size: 1.15 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'

        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

40 entries across 40 versions & 4 rubygems

Version Path
activeadmin-rb-1.5.2 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.8.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-rb-1.5.1 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.7.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.6.1 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.6.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.5.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.4.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.3.1 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.3.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.2.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.1.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.0.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-rb-1.5.0 lib/active_admin/resource_controller/streaming.rb
activeadmin-2.0.0.rc2 lib/active_admin/resource_controller/streaming.rb
activeadmin_addons-1.7.1 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource_controller/streaming.rb
activeadmin-2.0.0.rc1 lib/active_admin/resource_controller/streaming.rb
activeadmin-rb-1.4.0 lib/active_admin/resource_controller/streaming.rb
activeadmin_addons-1.7.0 vendor/bundle/ruby/2.3.0/bundler/gems/activeadmin-f71b375325eb/lib/active_admin/resource_controller/streaming.rb
activeadmin-1.4.3 lib/active_admin/resource_controller/streaming.rb