Skip to content

openfirmware/acts-as-messageable

 
 

Repository files navigation

ActsAsMessageable

The Acts As Messageable allows communication between the models.

Usage

To use it, add it to your Gemfile:

gem 'acts-as-messageable'

Post instalation

rails g acts-as-messageable:migration table_name # default 'messages'
rake db:migrate

Usage

class User < ActiveRecord::Base
  acts_as_messageable :table_name => "table_with_messages", # default 'messages'
                      :required   => :body                  # default [:topic, :body]
  end

Send message

@alice = User.first
@bob   = User.last

@alice.send_message(@bob, "Message topic", "Hi bob!")
@bob.send_message(@alice, "Re: Message topic", "Hi alice!")

With hash

@alice.send_message(@bob, { :body => "Hash body", :topic => "Hash topic" })

Custom required

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")

Required sequence

class User < ActiveRecord::Base
  acts_as_messageable :required => [:body, :topic]
end

@alice.send_message(@bob, "body", "topic")

Conversation

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

New API

@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

Delete message

@alice.messages.process do |message|
    message.delete
end

Copyright © 2011 Piotr Niełacny (ruby-blog.pl), released under the MIT license

Packages

No packages published

Languages

  • Ruby 100.0%