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



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

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



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

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

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

  return conv
end

#empty_trash(options = {}) ⇒ Object

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



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

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

#has_conversation?(conversation) ⇒ Boolean

Returns if messageable is a participant of conversation

Returns:

  • (Boolean)


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

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

#inbox(options = {}) ⇒ Object

Returns the conversations in the inbox of messageable

Same as conversations(=> ‘inbox’)



55
56
57
58
# File 'app/models/mailbox.rb', line 55

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

#is_completely_trashed?(conversation) ⇒ Boolean

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

Returns:

  • (Boolean)


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

def is_completely_trashed?(conversation)
  return 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)


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

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

#notifications(options = {}) ⇒ Object

Returns the notifications for the messageable



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

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

#receipts(options = {}) ⇒ Object

Returns all the receipts of messageable, from Messages and Notifications



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

def receipts(options = {})
  return 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



110
111
112
113
114
115
116
117
118
119
# File 'app/models/mailbox.rb', line 110

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

#sentbox(options = {}) ⇒ Object

Returns the conversations in the sentbox of messageable

Same as conversations(=> ‘sentbox’)



63
64
65
66
# File 'app/models/mailbox.rb', line 63

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

#trash(options = {}) ⇒ Object

Returns the conversations in the trash of messageable

Same as conversations(=> ‘trash’)



71
72
73
74
# File 'app/models/mailbox.rb', line 71

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