Sha256: b23b42e9c099a56b94569f6ddb3cae71ae81342422ebab41c8b511b81de011fd

Contents?: true

Size: 836 Bytes

Versions: 61

Compression:

Stored size: 836 Bytes

Contents

# 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

61 entries across 61 versions & 1 rubygems

Version Path
better_record-0.11.4 lib/better_record/batches.rb
better_record-0.10.10 lib/better_record/batches.rb
better_record-0.10.9 lib/better_record/batches.rb
better_record-0.10.8 lib/better_record/batches.rb
better_record-0.10.7 lib/better_record/batches.rb
better_record-0.10.6 lib/better_record/batches.rb
better_record-0.10.5 lib/better_record/batches.rb
better_record-0.10.4 lib/better_record/batches.rb
better_record-0.10.3 lib/better_record/batches.rb
better_record-0.10.2 lib/better_record/batches.rb
better_record-0.10.1 lib/better_record/batches.rb
better_record-0.10.0 lib/better_record/batches.rb
better_record-0.9.3 lib/better_record/batches.rb
better_record-0.9.1 lib/better_record/batches.rb
better_record-0.9.0 lib/better_record/batches.rb
better_record-0.8.6 lib/better_record/batches.rb
better_record-0.8.5 lib/better_record/batches.rb
better_record-0.8.4 lib/better_record/batches.rb
better_record-0.8.3 lib/better_record/batches.rb
better_record-0.8.2 lib/better_record/batches.rb