Module: Locomotive::Concerns::ContentEntry::Csv::ClassMethods

Defined in:
app/models/locomotive/concerns/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
76
77
78
79
# File 'app/models/locomotive/concerns/content_entry/csv.rb', line 58

def to_csv(options = {})
  # puts "to_csv #{all.count.inspect} / #{all.first.inspect}"
  # puts "#{all.first.content_type_id.inspect}"
  # puts "#{all.first.content_type.inspect}"
  # binding.pry
  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