Class: Mailbox

Inherits:
Object
  • Object
show all
Defined in:
app/models/mailbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messageable) ⇒ Mailbox

Initializer method



6
7
8
# File 'app/models/mailbox.rb', line 6

def initialize(messageable)
  @messageable = messageable
end

Instance Attribute Details

#messageableObject (readonly)

Returns the value of attribute messageable.



3
4
5
# File 'app/models/mailbox.rb', line 3

def messageable
  @messageable
end

#typeObject

Returns the value of attribute type.



2
3
4
# File 'app/models/mailbox.rb', line 2

def type
  @type
end

Instance Method Details

#conversations(options = {}) ⇒ Object

Returns the conversations for the messageable

Options

  • :mailbox_type

  • “inbox”

  • “sentbox”

  • “trash”

  • :read=false

  • :unread=true



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/mailbox.rb', line 33

def conversations(options = {})
  conv = Conversation.participant(@messageable)

  if options[:mailbox_type].present?
    case options[:mailbox_type]
    when 'inbox'
      conv = Conversation.inbox(@messageable)
    when 'sentbox'
      conv = Conversation.sentbox(@messageable)
    when 'trash'
      conv = Conversation.trash(@messageable)
    when  'not_trash'
      conv = Conversation.not_trash(@messageable)
    end
  end

  if (options.has_key?(:read) && options[:read]==false) || (options.has_key?(:unread) && options[:unread]==true)
    conv = conv.unread(@messageable)
  end

  conv
end

#empty_trash(options = {}) ⇒ Object

Deletes all the messages in the trash of messageable. NOT IMPLEMENTED.



86
87
88
89
# File 'app/models/mailbox.rb', line 86

def empty_trash(options = {})
  #TODO
  false
end

#has_conversation?(conversation) ⇒ Boolean

Returns if messageable is a participant of conversation

Returns:

  • (Boolean)


92
93
94
# File 'app/models/mailbox.rb', line 92

def has_conversation?(conversation)
  conversation.is_participant?(@messageable)
end

#inbox(options = {}) ⇒ Object

Returns the conversations in the inbox of messageable

Same as conversations(=> ‘inbox’)



59
60
61
62
# File 'app/models/mailbox.rb', line 59

def inbox(options={})
  options = options.merge(:mailbox_type => 'inbox')
  self.conversations(options)
end

#is_completely_trashed?(conversation) ⇒ Boolean

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

Returns:

  • (Boolean)


102
103
104
# File 'app/models/mailbox.rb', line 102

def is_completely_trashed?(conversation)
  conversation.is_completely_trashed?(@messageable)
end

#is_trashed?(conversation) ⇒ Boolean

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

Returns:

  • (Boolean)


97
98
99
# File 'app/models/mailbox.rb', line 97

def is_trashed?(conversation)
  conversation.is_trashed?(@messageable)
end

#notifications(options = {}) ⇒ Object

Returns the notifications for the messageable



11
12
13
14
15
16
17
18
19
# File 'app/models/mailbox.rb', line 11

def notifications(options = {})
  #:type => nil is a hack not to give Messages as Notifications
  notifs = Message.recipient(@messageable).order("messages.created_at DESC")
  if (options[:read].present? and options[:read]==false) or (options[:unread].present? and options[:unread]==true)
    notifs = notifs.unread
  end

  notifs
end

#receipts(options = {}) ⇒ Object

Returns all the receipts of messageable, from Messages and Notifications



81
82
83
# File 'app/models/mailbox.rb', line 81

def receipts(options = {})
  Receipt.where(options).recipient(@messageable)
end

#receipts_for(object) ⇒ Object

Returns the receipts of object for messageable as a ActiveRecord::Relation

Object can be:

  • A Message

  • A Notification

  • A Conversation

If object isn’t one of the above, a nil will be returned



114
115
116
117
118
119
120
121
# File 'app/models/mailbox.rb', line 114

def receipts_for(object)
  case object
  when Message#, Notification
    object.receipt_for(@messageable)
  when Conversation
    object.receipts_for(@messageable)
  end
end

#sentbox(options = {}) ⇒ Object

Returns the conversations in the sentbox of messageable

Same as conversations(=> ‘sentbox’)



67
68
69
70
# File 'app/models/mailbox.rb', line 67

def sentbox(options={})
  options = options.merge(:mailbox_type => 'sentbox')
  self.conversations(options)
end

#trash(options = {}) ⇒ Object

Returns the conversations in the trash of messageable

Same as conversations(=> ‘trash’)



75
76
77
78
# File 'app/models/mailbox.rb', line 75

def trash(options={})
  options = options.merge(:mailbox_type => 'trash')
  self.conversations(options)
end