Mongoid List
Mongoid List uses a position column to maintain an ordered list, with optional scoping. It uses atomic updates to keep lists in either a Collection or Embedded in sync.
Installation
Add to your Gemfile:
gem 'mongoid-list'
Usage
Add a list:
class CrowTRobot
include Mongoid::Document
include Mongoid::List
lists
end
Available methods:
doc1.position
doc2.position
doc1.position = 2
doc1.save
doc1.position
doc2.position
Klass.update_positions_in_list!(elements)
Pass in all elements in new ordering. Accepts documents or ids.
doc.list_scoped?
doc.list_scope_field
doc.list_scope_value
doc.list_scope_conditions
Scoping
To scope the list, pass :scope
on lists definition:
class TomServo
include Mongoid::Document
include Mongoid::List
lists scope: :satellite_of_love_id
belongs_to :satellite_of_love
end
TO-DO
- Helper methods to move individual documents within the list.
- Customizable filed name.