Class: Mailboxer::Mailbox

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messageable) ⇒ Mailbox

Initializer method


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

def initialize(messageable)
  @messageable = messageable
end

Instance Attribute Details

#messageableObject (readonly)

Returns the value of attribute messageable


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

def messageable
  @messageable
end

Instance Method Details

#conversations(options = {}) ⇒ Object

Returns the conversations for the messageable

Options

  • :mailbox_type

  • “inbox”

  • “sentbox”

  • “trash”

  • :read=false

  • :unread=true


32
33
34
35
36
37
38
39
40
# File 'app/models/mailboxer/mailbox.rb', line 32

def conversations(options = {})
  conv = get_conversations(options[:mailbox_type])

  if options[:read] == false || options[:unread]
    conv = conv.unread(messageable)
  end

  conv
end

#empty_trash(options = {}) ⇒ Object

Deletes all the messages in the trash of messageable.


72
73
74
75
76
# File 'app/models/mailboxer/mailbox.rb', line 72

def empty_trash(options = {})
  trash(options).each do |conversation|
    conversation.mark_as_deleted(messageable)
  end
end

#has_conversation?(conversation) ⇒ Boolean

Returns if messageable is a participant of conversation

Returns:

  • (Boolean)

79
80
81
# File 'app/models/mailboxer/mailbox.rb', line 79

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

#inbox(options = {}) ⇒ Object

Returns the conversations in the inbox of messageable

Same as conversations(=> 'inbox')


45
46
47
48
# File 'app/models/mailboxer/mailbox.rb', line 45

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

#is_completely_trashed?(conversation) ⇒ Boolean

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

Returns:

  • (Boolean)

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

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)

84
85
86
# File 'app/models/mailboxer/mailbox.rb', line 84

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

#notifications(options = {}) ⇒ Object

Returns the notifications for the messageable


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

def notifications(options = {})
  #:type => nil is a hack not to give Messages as Notifications
  notifs = Mailboxer::Notification.recipient(messageable).where(:type => nil).order("mailboxer_notifications.created_at DESC")
  if options[:read] == false || options[:unread]
    notifs = notifs.unread
  end

  notifs
end

#receipts(options = {}) ⇒ Object

Returns all the receipts of messageable, from Messages and Notifications


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

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


101
102
103
104
105
106
107
108
# File 'app/models/mailboxer/mailbox.rb', line 101

def receipts_for(object)
  case object
  when Mailboxer::Message, Mailboxer::Notification
    object.receipt_for(messageable)
  when Mailboxer::Conversation
    object.receipts_for(messageable)
  end
end

#sentbox(options = {}) ⇒ Object

Returns the conversations in the sentbox of messageable

Same as conversations(=> 'sentbox')


53
54
55
56
# File 'app/models/mailboxer/mailbox.rb', line 53

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

#trash(options = {}) ⇒ Object

Returns the conversations in the trash of messageable

Same as conversations(=> 'trash')


61
62
63
64
# File 'app/models/mailboxer/mailbox.rb', line 61

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