Mongoid Listable

Code Climate Build Status Coverage Status Gem Version

Mongoid Listable manages lists for isolated collections or for more complex has_many / belongs_to relationships. There are two main macros:

listed for isolated lists not belonging to any parent object.
lists for has_many / belongs_to relationships

Basic Usage - Isolated

class Photo
  include Mongoid::Document
  include Mongoid::Listable

  listed
end

The listed macro will assign a position field and a list scope to the Photo class. All Photo instances added or removed, (or whose position fields are updated) will trigger automatic reording of all sibling instances.

Basic Usage - Has Many

class User
  include Mongoid::Document
  include Mongoid:Listable

  has_many :photos
  lists :photos
  ...
end

class Photo
  include Mongoid::Document

  belongs_to :user
  ...
end

In this example photos that are added to or removed from a user, or have their position attribute updated will maintain logical order based on the method used:

# setter
User.first.photos = [ photo_a, photo_c, photo_b ]
User.first.photos.last == photo_b #=> true

# ids setter
User.first.photo_ids = [ '527fe97c67df6f07e1000003', '527fe97c67df6f07e1000004', '527fe97c67df6f07e1000003' ]
User.first.photos[1].id == '527fe97c67df6f07e1000004' #=> true

# push
photo = Photo.create
User.first.photos << photo
User.first.photos.last == photo #=> true

Each photo that belongs to the user will automatically obtain a field called user_position. The field is derived from the foreign key of the relation, replacing "_id" with "_position".

The has_many relation of a user to their photos will automatically be ordered by user_position unless otherwise specified via the standard order option to the has_many macro.

Advanced Usage - Has Many

# Handles multiple has_many relations on same model!

class User
  include Mongoid::Document
  include Mongoid:Listable

  has_many :featured_photos, 
  class_name: 'Photo', 
  inverse_of: :featured_by_user, 
  foreign_key: :featured_by_user_id

  has_many :kodak_moments, 
  class_name: 'Photo', 
  inverse_of: :kodaked_by_user, 
  foreign_key: :kodaked_by_user_id

  lists :featured_photos
  lists :kodak_moments
  ...
end

class Photo
  include Mongoid::Document

  belongs_to :featured_by_user, 
  class_name: 'User', 
  inverse_of: featured_photos, 
  foreign_key: :featured_by_user_id

  belongs_to :kodaked_by_user, 
  class_name: 'User', 
  inverse_of: kodak_moments, 
  foreign_key: :kodaked_by_user_id
  ...
end

In this example, there are two has_many relations defined between a user and photos. Each photo belonging to a user will obtain two position fields: featured_by_user_position and kodaked_by_user_position.

Todo

Need to test embedded documents.

Installation

Add this line to your application's Gemfile:

gem 'mongoid_listable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongoid_listable

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request