29 lines
		
	
	
		
			844 B
		
	
	
	
		
			Ruby
		
	
	
	
			
		
		
	
	
			29 lines
		
	
	
		
			844 B
		
	
	
	
		
			Ruby
		
	
	
	
module  OrbitBasis
 | 
						|
  module BaseModel
 | 
						|
    extend ActiveSupport::Concern
 | 
						|
 | 
						|
    # included do
 | 
						|
    #   scope :recent, desc(:_id)
 | 
						|
    #   scope :exclude_ids, Proc.new { |ids| where(:_id.nin => ids.map(&:to_i)) }
 | 
						|
    #   scope :by_week, where(:created_at.gte => 7.days.ago.utc)
 | 
						|
    # end
 | 
						|
 | 
						|
    module ClassMethods
 | 
						|
 | 
						|
      def find_in_batches(opts = {})
 | 
						|
        batch_size = opts[:batch_size] || 1000
 | 
						|
        start = opts.delete(:start).to_i || 0
 | 
						|
        objects = self.limit(batch_size).skip(start)
 | 
						|
        t = Time.new
 | 
						|
        while objects.any?
 | 
						|
          yield objects
 | 
						|
          start += batch_size
 | 
						|
          # Rails.logger.debug("processed #{start} records in #{Time.new - t} seconds") if Rails.logger.debug?
 | 
						|
          break if objects.size < batch_size
 | 
						|
          objects = self.limit(batch_size).skip(start)
 | 
						|
        end
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |