Sha256: 74e9a8040ab0750cb8176c6f94023363bb3646e251468dd23c189f49ff3c108d

Contents?: true

Size: 854 Bytes

Versions: 8

Compression:

Stored size: 854 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module BetterRecord
  module Batches
    def split_batches(options = {}, &block)
      options.assert_valid_keys(:start, :batch_size, :preserve_order)
      if block_given? && arel.orders.present? && options[:preserve_order]
        relation = self
        offset = options[:start] || 0
        batch_size = options[:batch_size] || 1000

        total = relation.count(:*)
        records = relation.limit(batch_size).offset(offset).to_a
        while records.any?
          records_size = records.size

          block.call records

          break if records_size < batch_size
          offset += batch_size
          records = relation.limit(batch_size).offset(offset).to_a
        end
        nil
      else
        find_in_batches(options.except(:preserve_order), &block)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
better_record-0.22.6 lib/better_record/batches.rb
better_record-0.22.5 lib/better_record/batches.rb
better_record-0.22.4 lib/better_record/batches.rb
better_record-0.22.3 lib/better_record/batches.rb
better_record-0.21.3 lib/better_record/batches.rb
better_record-0.22.2 lib/better_record/batches.rb
better_record-0.22.0 lib/better_record/batches.rb
better_record-0.21.2 lib/better_record/batches.rb