Module: Locomotive::Extensions::ContentEntry::Csv::ClassMethods

Defined in:
app/models/locomotive/extensions/content_entry/csv.rb

Instance Method Summary collapse

Instance Method Details

#to_csv(options = {}) ⇒ String

Generate a csv from a collection of content entries

Parameters:

  • options (Hash) (defaults to: {})

    Options for the csv generation

Returns:

  • (String)

    The well-generated CSV document



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/locomotive/extensions/content_entry/csv.rb', line 58

def to_csv(options = {})
  content_type  = options.delete(:content_type) || all.first.try(:content_type)
  csv_options   = options.select do |k, v|
    CSV::DEFAULT_OPTIONS.keys.include?(k.to_sym)
  end

  fields = content_type.ordered_entries_custom_fields
  labels = fields.map(&:label) << I18n.t('mongoid.attributes.locomotive/content_entry.created_at')

  CSV.generate(csv_options) do |csv|
    # header
    csv << labels
    # body
    all.each_by(100) do |entry|
      csv << entry.to_values(options)
    end
  end
end