class ActiveModel::ArrayExporter
Attributes
collection[R]
exporter[R]
scope[R]
Public Class Methods
new(collection, options = {})
click to toggle source
# File lib/active_model/array_exporter.rb, line 5 def initialize(collection, options = {}) @collection = Array(collection) @scope = options.delete(:scope) @exporter = options.delete(:exporter) end
Public Instance Methods
to_csv()
click to toggle source
# File lib/active_model/array_exporter.rb, line 11 def to_csv generate_file end
to_xls()
click to toggle source
# File lib/active_model/array_exporter.rb, line 15 def to_xls generate_file(col_sep: "\t") end
Private Instance Methods
exporter_for(object)
click to toggle source
# File lib/active_model/array_exporter.rb, line 30 def exporter_for(object) exporter_class = exporter || Exporter.exporter_for(object) exporter_class.new(object, scope: scope) end
generate_file(options = {})
click to toggle source
# File lib/active_model/array_exporter.rb, line 21 def generate_file(options = {}) CSV.generate(**options) do |file| file << headers collection.each do |object| file << exporter_for(object).values end end end
headers()
click to toggle source
# File lib/active_model/array_exporter.rb, line 35 def headers object = collection.first attributes = exporter_for(object).attributes if object.class.respond_to?(:human_attribute_name) attributes.map { |attr| object.class.human_attribute_name(attr) } else attributes.map(&:to_s) end end