Unread-Mongoid

Ruby gem to manage read/unread status of Mongoid objects.

Credit

First and foremost this is a fork of Unread by Georg Ledermann. If you don't have to use Mongo for this I highly reccomend you use his gem, this is a task much better suited to a relational db.

Features

  • Manages unread records for anything you want users to read (like messages, documents, comments etc.)
  • Supports mark as read to mark a single record as read
  • Supports mark all as read to mark all records as read in a single step
  • Gives you a scope to get the unread or read records for a given user
  • Needs only one additional collection

Requirements

  • Ruby 1.8.7 or 1.9.3 or 2.0.0
  • Rails 3 (including 3.0, 3.1, 3.2) and Rails 4.
  • Needs a timestamp field in your models (like created_at or updated_at) with a database index on it

Installation

Step 1: Add this to your Gemfile:

gem 'unread-mongoid'

and run

bundle

Usage

class User
  include Mongoid::Document
  include UnreadMongoid

  acts_as_reader
end

class Message
  include Mongoid::Document
  include Mongoid::Timestamps

  include UnreadMongoid
  acts_as_readable :on => :created_at
end

message1 = Message.create!
message2 = Message.create!

## Get unread messages for a given user
Message.unread_by(current_user).entries
# => [ message1, message2 ]

message1.mark_as_read! :for => current_user
Message.unread_by(current_user).entries
# => [ message2 ]

Message.mark_as_read! :all, :for => current_user
Message.unread_by(current_user).entries
# => [ ]

# Optional: Cleaning up unneeded markers.
# Do this in a cron job once a day.
Message.cleanup_read_marks!