The Acts As Messageable allows communication between the models.
To use it, add it to your Gemfile:
gem 'acts-as-messageable'
rails g acts-as-messageable:migration table_name # default 'messages' rake db:migrate
class User < ActiveRecord::Base acts_as_messageable :table_name => "table_with_messages", # default 'messages' :required => :body # default [:topic, :body] end
@alice = User.first @bob = User.last @alice.send_message(@bob, "Message topic", "Hi bob!") @bob.send_message(@alice, "Re: Message topic", "Hi alice!")
@alice.send_message(@bob, { :body => "Hash body", :topic => "Hash topic" })
In User model
class User < ActiveRecord::Base acts_as_messageable :required => :body end @alice.send_message(@bob, { :body => "Hash body" })
or
@alice.send_message(@bob, "body")
class User < ActiveRecord::Base acts_as_messageable :required => [:body, :topic] end @alice.send_message(@bob, "body", "topic")
@message = @alice.send_message(@bob, "Hello bob!", "How are you?") @reply_message = @bob.reply_to(@message, "Re: Hello bob!", "I'm fine!") @message.conversation #=> [@message, @reply_message] @reply_message.conversation #=> [@message, @reply_message]
@alice.messages # all messages connected with @alice (inbox/outbox) @alice.received_messages # @alice inbox @alice.sent_messages # @alice outbox @alice.deleted_messages # all messages connected with @alice (trash) @alice.messages.are_from(@bob) # all message form @bob @alice.messages.are_to(@bob) # all message to @bob @alice.messages.with_id(@id_of_message) # message with id id_of_message @alice.messages.readed # all readed @alice messages @alice.messages.unaded # all unreaded @alice messages
You can use multiple filters at the same time
@alice.messages.are_from(@bob).are_to(@alice).readed # all message from @bob to @alice and readed @alice.deleted_messages.are_from(@bob) # all deleted messages from @bob
@alice.messages.process do |message| message.delete end
Copyright © 2011 Piotr Niełacny (ruby-blog.pl), released under the MIT license