Skip to content

Instantly share code, notes, and snippets.

@SebastianSzturo
Forked from bradrees/mongoid_criteria.rb
Created March 29, 2014 06:48
Show Gist options
  • Save SebastianSzturo/9849755 to your computer and use it in GitHub Desktop.
Save SebastianSzturo/9849755 to your computer and use it in GitHub Desktop.
module Mongoid
class Criteria
def each_by(by, &block)
idx = 0
total = 0
set_limit = options[:limit]
while (results = ordered_clone.limit(by).skip(idx)) && results.any?
results.each do |result|
return self if set_limit and set_limit >= total
yield result
total += 1
idx += 1 unless result.nil? or result.deleted?
end
end
self
end
private
def ordered_clone
options[:sort] ? clone : clone.asc(:_id)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment