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.

Build Status Dependency Status Code Climate Coverage Status Gem Version

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
end

Send 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
end

With 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 messages

Inbox

@alice.received_messages

Outbox

@alice.sent_messages

Inbox + Outbox. All messages connected with @alice

@alice.messages

Trash

@alice.deleted_messages

Filters

==========

@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 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

Read messages

Read message

@message.open # open message
@message.read
@message.mark_as_read

Unread message

@message.close # unread message
@message.mark_as_unread

Delete 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
end

Now 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 @message

Restore message

@alice.deleted_messages.process do |m|
  m.restore # @alice restore 'm' message from trash
end

Restore message without context

@alice.restore_message(@message) # @alice restore message from trash

Search

Search text from messages

@alice.messages.search("Search me")  @alice seach text "Search me" from all messages

Gem

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