The Acts As Messageable allows communication between the models.
To use it, add it to your Gemfile:
gem 'acts-as-messageable'
Use this fork Thanks for @openfirmware
gem 'acts-as-messageable', :git => 'git://github.com/openfirmware/acts-as-messageable.git',
:branch => 'rails2.3.11_compatible'
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]
:class_name => "CustomMessages" # default "ActsAsMessageable::Message",
:dependent => :destroy # default :nullify
:group_messages => true # default false
end
Just type once again
rails g acts-as-messageable:migration
And new migrations should be created.
~$ rails g acts-as-messageable:migration
create db/migrate/20110811223435_add_recipient_permanent_delete_and_sender_permanent_delete_to_messages.rb
@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" })
@alice.send_message(@bob, "body")
class User < ActiveRecord::Base
acts_as_messageable :required => [:body, :topic]
end
@alice.send_message(@bob, "body", "topic")
class User < ActiveRecord::Base
acts_as_messageable :required => [:topic, :body]
end
@alice.send_message(@bob, "topic", "body")
You can use your own class that will represent the message object. First of all create custom class
class CustomMessage < ActsAsMessageable::Message
def capitalize_title
title.capitalize
end
end
After that you can sepcify custom class in options.
class User
acts_as_messageable :class_name => "CustomMessage"
end
From now on, your message has custom class.
@message = @alice.send_message(@bob, "hi!")
@message # => #<CustomMessage:0x000000024b6278>
@message.capitalize_title # => "Hi!"
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!")
@message.conversation #=> [@message, @reply_message]
@reply_message.conversation #=> [@message, @reply_message]
You can search text from messages and get the records where match exist. For example:
records = @alice.messages.search("Search me") @alice seach text "Search me" from all messages
@alice.received_messages
@alice.sent_messages
@alice.messages
@alice.deleted_messages
==========
@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
@message.open # open message
@message.read
@message.mark_as_read
@message.close # unread message
@message.mark_as_unread
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
@alice.delete_message(@message) # @alice delete @message
@alice.deleted_messages.process do |m|
m.restore # @alice restore 'm' message from trash
end
@alice.restore_message(@message) # @alice restore message from trash
class User
acts_as_messageable :group_messages => true
end
@message = @alice.send_message(@bob, :topic => "Helou bob!", :body => "What's up?")
@reply_message = @sukhi.reply_to(@message, "Hi there!", "I would like to join to this conversation!")
@sec_reply_message = @bob.reply_to(@message, "Hi!", "Fine!")
@third_reply_message = @alice.reply_to(@reply_message, "hi!", "no problem")
@message.people # will give you participants users object
@message.people # => [@alice, @bob, @sukhi]
@alice.messages.search("Search me") @alice seach text "Search me" from all messages
Copyright © 2011-2012 Piotr Niełacny (http://ruby-blog.pl), released under the MIT license