diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-20 16:49:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-20 16:49:51 +0300 |
commit | 71786ddc8e28fbd3cb3fcc4b3ff15e5962a1c82e (patch) | |
tree | 6a2d93ef3fb2d353bb7739e4b57e6541f51cdd71 /lib/csv_builder.rb | |
parent | a7253423e3403b8c08f8a161e5937e1488f5f407 (diff) |
Add latest changes from gitlab-org/gitlab@15-9-stable-eev15.9.0-rc42
Diffstat (limited to 'lib/csv_builder.rb')
-rw-r--r-- | lib/csv_builder.rb | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/csv_builder.rb b/lib/csv_builder.rb index f270f7984da..a54c355396d 100644 --- a/lib/csv_builder.rb +++ b/lib/csv_builder.rb @@ -23,15 +23,17 @@ class CsvBuilder # # * +collection+ - The data collection to be used # * +header_to_hash_value+ - A hash of 'Column Heading' => 'value_method'. + # * +associations_to_preload+ - An array of records to preload with a batch of records. # # The value method will be called once for each object in the collection, to # determine the value for that row. It can either be the name of a method on # the object, or a lamda to call passing in the object. - def initialize(collection, header_to_value_hash) + def initialize(collection, header_to_value_hash, associations_to_preload = []) @header_to_value_hash = header_to_value_hash @collection = collection @truncated = false @rows_written = 0 + @associations_to_preload = associations_to_preload end # Renders the csv to a string @@ -75,7 +77,13 @@ class CsvBuilder protected def each(&block) - @collection.find_each(&block) # rubocop: disable CodeReuse/ActiveRecord + if @associations_to_preload.present? && @collection.respond_to?(:each_batch) + @collection.each_batch(order_hint: :created_at) do |relation| + relation.preload(@associations_to_preload).order(:id).each(&block) # rubocop:disable CodeReuse/ActiveRecord + end + else + @collection.find_each(&block) # rubocop: disable CodeReuse/ActiveRecord + end end private |