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



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/locomotive/mongoid/patches.rb', line 137

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



124
125
126
127
128
129
130
# File 'lib/locomotive/mongoid/patches.rb', line 124

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



154
155
156
157
158
159
160
161
162
# File 'lib/locomotive/mongoid/patches.rb', line 154

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



164
165
166
# File 'lib/locomotive/mongoid/patches.rb', line 164

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

#without_sortingObject



132
133
134
# File 'lib/locomotive/mongoid/patches.rb', line 132

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