Module: CleanFindInBatches

Defined in:
lib/datatransit/clean_find_in_batches.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
# File 'lib/datatransit/clean_find_in_batches.rb', line 3

def self.included(base)
  base.class_eval do
    alias :old_find_in_batches :find_in_batches
    alias :find_in_batches :replacement_find_in_batches
  end
end

Instance Method Details

#replacement_find_in_batches(options = {}, &block) ⇒ Object

Override due to implementation of regular find_in_batches conflicting using UUIDs



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/datatransit/clean_find_in_batches.rb', line 12

def replacement_find_in_batches(options = {}, &block)
  relation = self
  return old_find_in_batches(options, &block) if relation.primary_key.is_a?(Arel::Attributes::Integer)
  # Throw errors like the real thing
  if (finder_options = options.except(:batch_size)).present?
    raise "You can't specify an order, it's forced to be #{batch_order}" if options[:order].present?
    raise "You can't specify a limit, it's forced to be the batch_size" if options[:limit].present?
    raise 'You can\'t specify start, it\'s forced to be 0 because the ID is a string' if options.delete(:start)
    relation = apply_finder_options(finder_options)
  end
  # Compute the batch size
  batch_size = options.delete(:batch_size) || 1000
  offset = 0
  # Get the relation and keep going over it until there's nothing left
  relation = relation.except(:order).order(batch_order).limit(batch_size)
  while (results = relation.offset(offset).limit(batch_size).all).any?
    block.call results
    offset += batch_size
  end
  nil
end