Class: Conversation

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/models/conversation.rb

Instance Method Summary collapse

Instance Method Details

#count_messagesObject

Returns the number of messages of the conversation



99
100
101
# File 'app/models/conversation.rb', line 99

def count_messages
	return Message.conversation(self).count
end

#is_completely_trashed?(participant) ⇒ Boolean

Returns true if the participant has trashed all the messages of the conversation

Returns:

  • (Boolean)


116
117
118
119
# File 'app/models/conversation.rb', line 116

def is_completely_trashed?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).trash.count == self.receipts_for(participant).count
end

#is_participant?(participant) ⇒ Boolean

Returns true if the messageable is a participant of the conversation

Returns:

  • (Boolean)


104
105
106
107
# File 'app/models/conversation.rb', line 104

def is_participant?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).count != 0
end

#is_read?(participant) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'app/models/conversation.rb', line 121

def is_read?(participant)
	!self.is_unread?(participant)
end

#is_trashed?(participant) ⇒ Boolean

Returns true if the participant has at least one trashed message of the conversation

Returns:

  • (Boolean)


110
111
112
113
# File 'app/models/conversation.rb', line 110

def is_trashed?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).trash.count!=0
end

#is_unread?(participant) ⇒ Boolean

Returns true if the participant has at least one unread message of the conversation

Returns:

  • (Boolean)


126
127
128
129
# File 'app/models/conversation.rb', line 126

def is_unread?(participant)
	return false if participant.nil?
	return self.receipts_for(participant).not_trash.is_unread.count!=0
end

#last_messageObject

Last message in the conversation.



88
89
90
91
# File 'app/models/conversation.rb', line 88

def last_message
	@last_message = self.messages.find(:first, :order => 'created_at DESC') if @last_message.nil?
	return @last_message
end

#last_senderObject

Sender of the last message.



82
83
84
85
# File 'app/models/conversation.rb', line 82

def last_sender
	@last_sender = self.last_message.sender if @last_sender.nil?
	return @last_sender
end

#mark_as_read(participant) ⇒ Object

Mark the conversation as read for one of the participants



31
32
33
34
# File 'app/models/conversation.rb', line 31

def mark_as_read(participant)
	return if participant.nil?
	return self.receipts_for(participant).mark_as_read
end

#mark_as_unread(participant) ⇒ Object

Mark the conversation as unread for one of the participants



37
38
39
40
# File 'app/models/conversation.rb', line 37

def mark_as_unread(participant)
	return if participant.nil?
	return self.receipts_for(participant).mark_as_unread
end

#move_to_trash(participant) ⇒ Object

Move the conversation to the trash for one of the participants



43
44
45
46
# File 'app/models/conversation.rb', line 43

def move_to_trash(participant)
	return if participant.nil?
	return self.receipts_for(participant).move_to_trash
end

#original_messageObject

First message of the conversation.



76
77
78
79
# File 'app/models/conversation.rb', line 76

def original_message
	@original_message = self.messages.find(:first, :order => 'created_at') if @original_message.nil?
	return @original_message
end

#originatorObject

Originator of the conversation.



70
71
72
73
# File 'app/models/conversation.rb', line 70

def originator
	@orignator = self.original_message.sender if @originator.nil?
	return @orignator
end

#participantsObject

Returns an array of participants



65
66
67
# File 'app/models/conversation.rb', line 65

def participants
  return recipients
end

#receipts_for(participant) ⇒ Object

Returns the receipts of the conversation for one participants



94
95
96
# File 'app/models/conversation.rb', line 94

def receipts_for(participant)
  return Receipt.conversation(self).recipient(participant)
end

#recipientsObject

Returns an array of participants



55
56
57
58
59
60
61
62
# File 'app/models/conversation.rb', line 55

def recipients
	if self.last_message
		recps = self.last_message.recipients
		recps = recps.is_a?(Array) ? recps : [recps]
	return recps
	end
	return []
end

#untrash(participant) ⇒ Object

Takes the conversation out of the trash for one of the participants



49
50
51
52
# File 'app/models/conversation.rb', line 49

def untrash(participant)
	return if participant.nil?
	return self.receipts_for(participant).untrash
end