Sha256: e16b8f2a5f06ab7405e53b1ca2562667745fc0f1d7dad8d7abdd0a9a04f3b2a1

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'action_controller'
require 'csv_rb/stream_csv_deflator'
unless Mime[:csv]
  Mime::Type.register 'text/csv', :csv
end

ActionController::Renderers.add :csv do |filename, options|
  #
  # You can always specify a template:
  #
  #  def called_action
  #    render csv: 'filename', template: 'controller/diff_action'
  #  end
  #
  # And the normal use case works:
  #
  #  def called_action
  #    render 'diff_action'
  #    # or
  #    render 'controller/diff_action'
  #  end
  #
  options[:template] = filename.gsub(/^.*\//,'') if options[:template] == action_name
  options[:template] = "#{options[:template]}.csv.csvrb" unless options[:template] =~ /\.csvrb/
  options[:layout] = false
  options[:locals] ||= {}
  file_name = "#{options.delete(:filename) || filename.gsub(/^.*\//,'')}#{options.delete(:with_time) ? "-#{Time.zone.now.to_s}" : ''}.csv".sub(/(\.csv)+$/, '.csv')


  # if options.delete(:should_stream)
    response.headers["X-Accel-Buffering"] = 'no'
    response.headers["Content-Type"] = "text/csv; charset=utf-8"
    response.headers["Content-Encoding"] = 'deflate'
    response.headers["Content-Disposition"] = %(attachment; filename="#{file_name}")


    return self.response_body = Enumerator.new do |y|
      csv = CSVRb::StreamCSVDeflator.new(y)
      instance_eval lookup_context.find_template(options[:template], options[:prefixes], options[:partial], options.dup.merge(formats: [:csv])).source
      csv.close
    end
  # else
  #   disposition = options.delete(:disposition) || 'attachment'
  #
  #   send_data render_to_string(options), filename: file_name, type: Mime[:csv], disposition: disposition
  # end
end

# For respond_to default
begin
  ActionController::Responder
rescue
else
  class ActionController::Responder
    def to_csv
      @_action_has_layout = false
      if @default_response
        @default_response.call(options)
      else
        controller.render({csv: controller.action_name}.merge(options))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_rb-0.5.2 lib/csv_rb/action_controller.rb