ActsAsChattable
The Acts As Chattable allows communication between models.
It was designed for a mobile app that needs private communications with attachments, like the iPhone SMS app for example.
Usage
To use it, add it to your Gemfile:
Rails 3 & 4
gem 'acts_as_chattable'Post instalation
rails g acts_as_chattable:migration
rake db:migrate
Usage
class User < ActiveRecord::Base
acts_as_chattable :required => :body # default [:body]
:dependent => :destroy # default :nullify
endSend message
@alice = User.first
@bob = User.last
@alice.send_message(@bob, "Hi bob!")
@bob.send_message(@alice, Hi alice!")With hash
@alice.send_message(@bob, { :body => "Hash body" })Custom required (validation)
In User model
class User < ActiveRecord::Base
acts_as_chattable :required => :body
endWith hash
@alice.send_message(@bob, { :body => "Hash body" })Normal
@alice.send_message(@bob, "body")Conversation
You can get conversation list from messages scope. For example:
@message = @alice.send_message(@bob, "Hello bob!", "How are you?")
@reply_message = @bob.reply_to(@message, "Re: Hello bob!", "I'm fine!")
@alice.received_messages.conversations # => [@reply_message]should receive list of latest messages in conversations (like in facebook).
To create conversation just reply to a message.
@message = @alice.send_message(@bob, "Hello bob!", "How are you?")
@message.reply("Re: Hello bob!", "I'm fine")Or with hash
@message.reply(:topic => "Re: Hello bob!", :body => "I'm fine")Or in old style
@message = @alice.send_message(@bob, "Hello bob!", "How are you?")
@reply_message = @bob.reply_to(@message, "Re: Hello bob!", "I'm fine!")Get conversation for a specific message
@message.conversation #=> [@message, @reply_message]
@reply_message.conversation #=> [@message, @reply_message]Search
You can search text from messages and get the records where match exist. For example:
Search text from messages
records = @alice.messages.search("Search me") @alice seach text "Search me" from all messagesInbox
@alice.received_messagesOutbox
@alice.sent_messagesInbox + Outbox. All messages connected with @alice
@alice.messagesTrash
@alice.deleted_messagesFilters
==========
@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.unreaded # all unreaded @alice messagesYou 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 @bobRead messages
Read message
@message.open # open message
@message.read
@message.mark_as_readUnread message
@message.close # unread message
@message.mark_as_unreadDelete message
We must know who delete message. That why we use .process method to save context
@message = @alice.send_message(@bob, "Topic", "Body")
@alice.messages.process do |message|
message.delete # @alice delete message
endNow we can find message in trash
@alice.deleted_messages #=> [@message]We can delete the message permanently
@alice.deleted_messages.process do |message|
message.delete
end
@alice.delete_message #=> []Message has been deleted permanently
Delete message without context
@alice.delete_message(@message) # @alice delete @messageRestore message
@alice.deleted_messages.process do |m|
m.restore # @alice restore 'm' message from trash
endRestore message without context
@alice.restore_message(@message) # @alice restore message from trashSearch
Search text from messages
@alice.messages.search("Search me") @alice seach text "Search me" from all messagesGem
rspec spec
rake gemspec
gem build acts_as_chattable.gemspec
gem push acts_as_chattable-0.0.x.gem
Copyright © 2013 Ben Bruscella, released under the MIT license