Class: Mongoid::List::Collection

Inherits:
Abstract
  • Object
show all
Defined in:
lib/mongoid/list/collection.rb

Instance Attribute Summary

Attributes inherited from Abstract

#obj

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#changes, #conditions, #initialize

Constructor Details

This class inherits a constructor from Mongoid::List::Abstract

Class Method Details

.update_positions!(klass, elements) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/mongoid/list/collection.rb', line 9

def update_positions!(klass, elements)
  elements.each_with_index do |element, idx|
    id = element.kind_of?(Hash) ? element['id'] : element
    if klass.using_object_ids?
      id = BSON::ObjectId.from_string(id) if id.is_a?(String)
    end
    klass.collection.find({ _id: id }).update({ '$set' => { position: (idx + 1) } })
  end
end

Instance Method Details

#countObject



28
29
30
# File 'lib/mongoid/list/collection.rb', line 28

def count
  obj.class.where(conditions).count
end

#update_positions!Object



22
23
24
25
26
# File 'lib/mongoid/list/collection.rb', line 22

def update_positions!
  obj.class.collection.find(criteria).update_all(
    { '$inc' => { position: changes[:by] } }
  )
end