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



37
38
39
40
41
42
43
44
45
# File 'app/models/mailboxer/mailbox.rb', line 37

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

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

  conv
end

#conversations_with(other_messageable) ⇒ Object

Returns the conversations between messageable and other messageable



21
22
23
# File 'app/models/mailboxer/mailbox.rb', line 21

def conversations_with(other_messageable)
  Mailboxer::Conversation.between(messageable, other_messageable)
end

#empty_trash(options = {}) ⇒ Object

Deletes all the messages in the trash of messageable.



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

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)


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

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

#inbox(options = {}) ⇒ Object

Returns the conversations in the inbox of messageable

Same as conversations(=> ‘inbox’)



50
51
52
53
# File 'app/models/mailboxer/mailbox.rb', line 50

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)


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

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)


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

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(:created_at => :desc, :id => :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



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

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



106
107
108
109
110
111
112
113
# File 'app/models/mailboxer/mailbox.rb', line 106

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



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

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



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

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