Module: HasMessages

Extended by:
ActiveSupport::Concern
Defined in:
lib/has_messages.rb

Overview

Adds a generic implementation for sending messages between users

Instance Method Summary collapse

Instance Method Details

#conversationsObject



38
39
40
# File 'lib/has_messages.rb', line 38

def conversations
  (messages + received_messages.map(&:message)).compact.uniq
end

#find_conversation_by_id(id) ⇒ Object



46
47
48
# File 'lib/has_messages.rb', line 46

def find_conversation_by_id(id)
  conversations.select{ |message| message.id == id.to_i }.first
end

#last_received_message_per_threadObject

Returns the most recent message of each thread



34
35
36
# File 'lib/has_messages.rb', line 34

def last_received_message_per_thread
  MessageRecipient.find_all_by_receiver_id(id, :order => 'id desc', :joins => :message, :conditions => 'message_recipients.hidden_at is null', :group => 'COALESCE(original_message_id,messages.id)')
end

#original_conversationsObject



42
43
44
# File 'lib/has_messages.rb', line 42

def original_conversations
  conversations.select{ |message| message.original_message_id == nil }
end

#sent_messagesObject

Composed messages that have already been sent. These consists of all messages that are currently in the “queued” or “sent” states.



29
30
31
# File 'lib/has_messages.rb', line 29

def sent_messages
  messages.with_states(:queued, :sent)
end

#unread_messagesObject



50
51
52
# File 'lib/has_messages.rb', line 50

def unread_messages
  received_messages.select(&:unread?).map(&:message)
end

#unsent_messagesObject

Composed messages that have not yet been sent. These consists of all messages that are currently in the “unsent” state.



23
24
25
# File 'lib/has_messages.rb', line 23

def unsent_messages
  messages.with_state(:unsent)
end