mirror of
https://github.com/gitlabhq/gitlabhq.git
synced 2025-07-23 02:54:40 +00:00
CsvBuilder
Usage
Generate a CSV given a collection and a mapping.
columns = {
'Title' => 'title',
'Comment' => 'comment',
'Author' => -> (post) { post.author.full_name }
'Created At (UTC)' => -> (post) { post.created_at&.strftime('%Y-%m-%d %H:%M:%S') }
}
CsvBuilder.new(@posts, columns).render
When the value of the mapping is a string, a method is called with the given name
on the record (for example: post.title
).
When the value of the mapping is a lambda, it is lazily executed.
It's possible to also pass ActiveRecord associations to preload when batching through the collection:
CsvBuilder.new(@posts, columns, [:author, :comments]).render
SingleBatch builder
When the collection is an array or enumerable you can use:
CsvBuilder::SingleBatch.new(@posts, columns).render
Stream builder
A stream builder uses a lazy and more efficient iterator and by default returns up to 100,000 records from the collection.
CsvBuilder::Stream.new(@posts, columns).render(1_000)
Development
Follow the GitLab gems development guidelines.