Darren Evans wrote:
Hi Phlip
Thanks very much for your reply, I really appreciate it. Mephisto looks
really interesting, but it's not exactly what I am looking for this
time.
I would like a system where one user sends a message to another user,
rather than one user sends a message to all users. Something like an
internal email system within the application is the best way that I can
describe it, but using user ids rather than e-mail addresses
Maybe I could adapt Mephisto by adding some conditions, or is there
something more appropriate?
Thanks
Darren
Phlip wrote:
Darren Evans wrote:
I don't really want to start re-inventing the wheel if there is an easy
solution.
A blog, right? Just install Mephisto or one of the others. Blogs with
message boards are where it's at. Even simpler, a Wiki. The Recent
Changes is the daily inbox.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!
Yup, I've implemented one. Really easy to roll your own, particularly if you're using REST. My model is really simple, with just a few validations and utility methods:
class Message < ActiveRecord::Base
validates_presence_of :title, :body, :recipient_id, :sender_id
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
belongs_to :recipient, :class_name => "User", :foreign_key => "recipient_id"
attr_accessible :title, :body, :recipient_id
# aliases read_at accessor attribute to make it read more nicely
def read?
read_at
end
# returns the opposite_party in the messsage to given user. So if the given user is the recipient,
# this method returns the sender and vice versa. If the given user is neither a recipient nor sender
# (e.g. because they're an editor) it returns the recipient. If the given user is nil (e.g. because
# the current_user is nil) an exception is raised
def partner_to(user)
raise ArgumentError unless user
user.id == recipient_id ? sender : recipient
end
end
In User model (which is based on acts_as_authenticated), I've got the following associations:
has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id", :order => 'created_at DESC'
has_many :received_messages, :class_name => "Message", :foreign_key => "recipient_id", :conditions => ['deleted_at IS NULL'], :order => 'created_at DESC'
has_many :unread_messages, :class_name => "Message", :foreign_key => "recipient_id", :conditions => ['read_at IS NULL AND deleted_at IS NULL'], :order => 'created_at DESC'
The schema for the Message class is:
create_table "messages", :force => true do |t|
t.column "title", :string
t.column "body", :text
t.column "recipient_id", :integer
t.column "sender_id", :integer
t.column "created_at", :datetime
t.column "deleted_at", :datetime
t.column "read_at", :datetime
end
Hopefully should be enough to get you started.