This is a messaging system that is not bound to any friendship structure. Integrates directly into your User model. Allows for users to comment on a thread, create a thread, and be updated when a thread is commented on.
Install the gem
gem install acts_as_messagable
Generate the necessary classes and migrations (NOW WORKING AS OF version 0.4.0)
rails generate acts_as_messagable
Migrate the DB
Add acts_as_messagable to your user model
class User < ActiveRecord::Base ... acts_as_messagable ... end
A User class becomes acts_as_messagable:
class User acts_as_messagable . . end
Then User can send a message, and gets a thread back:
@shannon = User.first @ljuba = User.find_by_email('firstname.lastname@example.org') @branko = User.find_by_email('email@example.com') # Subject, Body, [recipients] newthread = @shannon.send_message("Ahoy there!", "Let's meet for lunch!", [@ljuba, @branko])
Messagables can view different types of messages they have:
@ljuba.sent_messages @ljuba.unread_messages @ljuba.read_messages
Receipts and MessageThreads
The author and all recipients get a Receipt for a message thread, except the author's thread is marked “read.” Any further comments on the thread will mark all receipts for that MessageThread as “unread.” This also allows a User to abandon a thread even if they start it, simply by deleting their Receipt for the MessageThread.
MessageThreads and Comments are not duplicated for each User. Viewing privileges, read/unread, trashiness ( :) ) are all taken care of via Receipts. User gets a single receipt for the whole thread, not individual Comments.
Therefore, once a receipt is destroyed, the original recipient of that receipt could no longer see replies to the thread at all. Note this is different behavior from e-mail. One could for example implement code to alert a sender in case everyone else has abandoned a thread; otherwise there is no way of knowing whether a message will be received (which is actually the case with e-mail anyway). In such a case the sender would have to start a new message thread.
MessageThread, Receipt, Comment, and some type of model - typically 'User' - that will be the “messagable” model are the models used.
Message Receipts have a “hidden” flag. One way to use this is for a Trash folder, where receipts would be marked “hidden” and “has_read”
Please see the RSpec tests for more guidance on usage.
All threads have a private_thread field. This allows the can_be_viewed_by? method to be utilized. The can_be_viewed_by? method checks to see if the object is either the author or one of the receipt objects:
j_random_user = User.find 490 thread.can_be_viewed_by?(j_random_user) => false ivana = User.find 301 thread.can_be_viewed_by?(ivana) => true
Changes since forking from acts_as_messenger 0.1 Copyright© 2011 Shannon E. Wells, under Creative Commons Attribution-ShareAlike 3.0 Unported License (creativecommons.org/licenses/by-sa/3.0/).
Copyright © 2010 Mike Nelson. See LICENSE for details.