Class: Receipt

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/receipt.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mark_as_read(options = {}) ⇒ Object

Marks all the receipts from the relation as read



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

def mark_as_read(options={})
  update_receipts({:is_read => true}, options)
end

.mark_as_unread(options = {}) ⇒ Object

Marks all the receipts from the relation as unread



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

def mark_as_unread(options={})
  update_receipts({:is_read => false}, options)
end

.move_to_inbox(options = {}) ⇒ Object

Moves all the receipts from the relation to inbox



52
53
54
# File 'app/models/receipt.rb', line 52

def move_to_inbox(options={})
  update_receipts({:mailbox_type => :inbox, :trashed => false}, options)
end

.move_to_sentbox(options = {}) ⇒ Object

Moves all the receipts from the relation to sentbox



57
58
59
# File 'app/models/receipt.rb', line 57

def move_to_sentbox(options={})
  update_receipts({:mailbox_type => :sentbox, :trashed => false}, options)
end

.move_to_trash(options = {}) ⇒ Object

Marks all the receipts from the relation as trashed



42
43
44
# File 'app/models/receipt.rb', line 42

def move_to_trash(options={})
  update_receipts({:trashed => true}, options)
end

.untrash(options = {}) ⇒ Object

Marks all the receipts from the relation as not trashed



47
48
49
# File 'app/models/receipt.rb', line 47

def untrash(options={})
  update_receipts({:trashed => false}, options)
end

.update_receipts(updates, options = {}) ⇒ Object

This methods helps to do a update_all with table joins, not currently supported by rails. Acording to the github ticket github.com/rails/rails/issues/522 it should be supported with 3.2.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/receipt.rb', line 64

def update_receipts(updates,options={})
  ids = Array.new
  where(options).each do |rcp|
    ids << rcp.id
  end
  return if ids.empty?
  conditions = [""].concat(ids)
  condition = "id = ? "
  ids.drop(1).each do
    condition << "OR id = ? "
  end
  conditions[0] = condition
  Receipt.except(:where).except(:joins).where(conditions).update_all(updates)
end

Instance Method Details

#conversationObject

Returns the conversation associated to the receipt if the notification is a Message



111
112
113
114
# File 'app/models/receipt.rb', line 111

def conversation
  return message.conversation if message.is_a? Message
  return nil
end

#is_trashed?Boolean

Returns if the participant have trashed the Notification

Returns:

  • (Boolean)


122
123
124
# File 'app/models/receipt.rb', line 122

def is_trashed?
  return self.trashed
end

#is_unread?Boolean

Returns if the participant have read the Notification

Returns:

  • (Boolean)


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

def is_unread?
  return !self.is_read
end

#mark_as_readObject

Marks the receipt as read



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

def mark_as_read
  update_attributes(:is_read => true)
end

#mark_as_unreadObject

Marks the receipt as unread



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

def mark_as_unread
  update_attributes(:is_read => false)
end

#move_to_inboxObject

Moves the receipt to inbox



101
102
103
# File 'app/models/receipt.rb', line 101

def move_to_inbox
  update_attributes(:mailbox_type => :inbox, :trashed => false)
end

#move_to_sentboxObject

Moves the receipt to sentbox



106
107
108
# File 'app/models/receipt.rb', line 106

def move_to_sentbox
  update_attributes(:mailbox_type => :sentbox, :trashed => false)
end

#move_to_trashObject

Marks the receipt as trashed



91
92
93
# File 'app/models/receipt.rb', line 91

def move_to_trash
  update_attributes(:trashed => true)
end

#untrashObject

Marks the receipt as not trashed



96
97
98
# File 'app/models/receipt.rb', line 96

def untrash
  update_attributes(:trashed => false)
end