Class: Mongoid::Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/mongoid/patches.rb

Instance Method Summary collapse

Instance Method Details

#each_by(by, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/locomotive/mongoid/patches.rb', line 30

def each_by(by, &block)
  idx = 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
      total += 1
      yield result
    end
    idx += by
  end
  self
end

#indexed_max(field) ⇒ Object

Optimized version of the max aggregate method. It works efficiently only if the field is part of a MongoDB index. more here: stackoverflow.com/questions/4762980/getting-the-highest-value-of-a-column-in-mongodb



47
48
49
50
51
52
53
54
55
# File 'lib/locomotive/mongoid/patches.rb', line 47

def indexed_max(field)
  _criteria = criteria.order_by(field.to_sym.desc).only(field.to_sym)
  selector  = _criteria.send(:selector_with_type_selection)
  fields    = _criteria.options[:fields]
  sort      = _criteria.options[:sort]

  document = collection.find(selector).select(fields).sort(sort).limit(1).first
  document ? document[field.to_s].to_i : nil
end

#to_liquidObject



57
58
59
# File 'lib/locomotive/mongoid/patches.rb', line 57

def to_liquid
  Locomotive::Liquid::Drops::ProxyCollection.new(self)
end

#without_sortingObject



25
26
27
# File 'lib/locomotive/mongoid/patches.rb', line 25

def without_sorting
  clone.tap { |crit| crit.options.delete(:sort) }
end