Class: Mongoid::Criteria

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

Overview

FIXME: the Origin (used by Steam) and Mongoid gems both modify the Symbol class to allow writing queries like .where(:position.lt => 1) By convention, Origin::Key will be the one. So we need to make sure it doesn’t break the Mongoid queries.

Defined Under Namespace

Modules: Queryable

Instance Method Summary collapse

Instance Method Details

#each_by(by, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/locomotive/mongoid/patches.rb', line 119

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

#first!Object



106
107
108
109
110
111
112
# File 'lib/locomotive/mongoid/patches.rb', line 106

def first!
  self.first.tap do |model|
    if model.nil?
      raise Mongoid::Errors::DocumentNotFound.new(self.klass, self.selector)
    end
  end
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



136
137
138
139
140
141
142
143
144
# File 'lib/locomotive/mongoid/patches.rb', line 136

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).projection(fields).sort(sort).limit(1).first
  document ? document[field.to_s].to_i : nil
end

#to_liquidObject



146
147
148
# File 'lib/locomotive/mongoid/patches.rb', line 146

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

#without_sortingObject



114
115
116
# File 'lib/locomotive/mongoid/patches.rb', line 114

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