module ActiveRecord
class Base
def self.each_by(group_size, options={}, &blk)
update = options.delete(:update) || false
num_records = count(options.except(:from))
return 0 if num_records == 0
also_pass_offset = (blk.arity == 2)
0.step(num_records, group_size) do |offset|
find_options = { :offset => offset, :limit => group_size }.merge(options)
if update
if num_records == 1
puts ">> Reading the only record."
else
start_offset = offset + 1
end_offset = offset + group_size
end_offset = num_records if num_records < end_offset
puts ">> Reading records #{start_offset}-#{end_offset}."
end
end
find(:all, find_options).each do |record|
also_pass_offset ? blk.call(record, offset) : blk.call(record)
end
end
num_records
end
end
end