Module: LpCSVExportable::WithStream

Defined in:
lib/lp_csv_exportable_with_stream/with_stream.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filename_prefixObject

Returns the value of attribute filename_prefix.



3
4
5
# File 'lib/lp_csv_exportable_with_stream/with_stream.rb', line 3

def filename_prefix
  @filename_prefix
end

#filename_suffixObject

Returns the value of attribute filename_suffix.



3
4
5
# File 'lib/lp_csv_exportable_with_stream/with_stream.rb', line 3

def filename_suffix
  @filename_suffix
end

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/lp_csv_exportable_with_stream/with_stream.rb', line 5

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#to_csv_lazy(formatter) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lp_csv_exportable_with_stream/with_stream.rb', line 9

def to_csv_lazy(formatter)
  formatter.headers['Content-Type'] = 'text/csv; charset=utf-8'
  formatter.headers['Content-Disposition'] =
    "attachment; filename=\"#{filename_prefix}#{self.class.filename}#{filename_suffix}.csv\""
  formatter.headers['X-Accel-Buffering'] = 'no'
  formatter.headers['Cache-Control'] = 'no-cache'
  formatter.headers['Last-Modified'] = Time.zone.now.ctime.to_s
  formatter.headers.delete('Content-Length')
  formatter.response_body = Enumerator.new do |yielder|
    yielder << "\uFEFF" + CSV.generate_line(headers).to_s.force_encoding('UTF-8')
    collection.includes(self.class.collection_includes || []).each do |obj|
      yielder << CSV.generate_line(columns.map do |column|
        column.format(retrieve_value(obj, column))
      end).to_s.force_encoding('UTF-8')
    end
  end
end