Sha256: 313ca0acb87293012babb4f8969a0887d78e8769571836571c57d1f3a9cb8f7d

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

# CloudXLSRails::CSVResponder
module CloudXLSRails
  class CSVResponder
    def initialize(controller, stream)
      @controller = controller
      @stream = stream
    end

    def stream!(filename)
      headers = @controller.headers
      headers['Last-Modified'] = Time.now.to_s
      headers["Content-Type"] = "text/csv"
      headers["Content-disposition"] = "attachment; filename=\"#{filename}\""
      # nginx doc: Setting this to "no" will allow unbuffered responses suitable
      # for Comet and HTTP streaming applications
      headers['X-Accel-Buffering'] = 'no'
      headers["Cache-Control"] ||= "no-cache"
      headers.delete("Content-Length")

      @controller.response.status = 200
      # setting the body to an enumerator, rails will iterate this enumerator
      @controller.response_body = @stream
    end

    def self.stream!(controller, scope, options)
      filename = options.fetch(:doc, {}).fetch(:filename, "data.csv")
      enum     = CloudXLS::CSVWriter.enumerator(scope, options)
      new(controller, enum).stream!(filename)
    end
  end
end


ActionController::Renderers.add :csv do |scope, options|
  filename = options.fetch(:filename, "data-#{DateTime.now.to_s}.csv")

  if options[:stream]
    CloudXLSRails::CSVResponder.stream!(self, scope, options)
  else # no stream:
    data = CloudXLS::CSVWriter.text(scope, options)

    send_data data,
      type: Mime::CSV,
      disposition: "attachment; filename=#{filename}.csv"
  end
end

class ActionController::Responder
  def to_csv
    if options[:stream] == true
      CloudXLSRails::CSVResponder.stream!(controller, resources.last, options)
    else
      controller.render({:csv => resources.last, :stream => false }.merge(options))
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cloudxls-rails-0.6.2 lib/cloudxls-rails/handlers/csv.rb
cloudxls-rails-0.6.1 lib/cloudxls-rails/handlers/csv.rb
cloudxls-rails-0.6.0 lib/cloudxls-rails/handlers/csv.rb
cloudxls-rails-0.5.0 lib/cloudxls-rails/handlers/csv.rb