Class: Discuss::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#draftObject

Returns the value of attribute draft.



7
8
9
# File 'app/models/discuss/message.rb', line 7

def draft
  @draft
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  !trashed? && !deleted?
end

#delete!Object



105
106
107
# File 'app/models/discuss/message.rb', line 105

def delete!
  update(deleted_at: Time.zone.now)
end

#draft?Boolean

passed in from the compose form

Returns:

  • (Boolean)


128
129
130
# File 'app/models/discuss/message.rb', line 128

def draft?
  self.draft == '1'
end

#mailboxObject



66
67
68
69
70
71
72
73
74
75
# File 'app/models/discuss/message.rb', line 66

def mailbox
  case
  when sent? then :outbox
  when received? then :inbox
  when !new_record? && unsent? then :drafts
  when trashed? then :trash
  else
    :compose
  end
end

#read!Object



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

def read!
  update(read_at: Time.zone.now) unless self.read_at.present?
end

#receive!Object



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

def receive!
  update(received_at: Time.zone.now)
end

#recipient_listObject



62
63
64
# File 'app/models/discuss/message.rb', line 62

def recipient_list
  draft_recipient_ids.reject(&:blank?).map {|id| User.find id}
end

#recipientsObject



54
55
56
# File 'app/models/discuss/message.rb', line 54

def recipients
  sent? ? children.collect(&:user) : parent.recipients
end

#recipients=(users) ⇒ Object



58
59
60
# File 'app/models/discuss/message.rb', line 58

def recipients= users
  users.each { |u| draft_recipient_ids << u.id }
end

#reply!(options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'app/models/discuss/message.rb', line 83

def reply! options={}
  if parent
    reply = children.create!(subject: options.fetch(:subject, subject),
                             body: options.fetch(:body, nil),
                             user: user,
                             recipients: [parent.user])
    reply.send!
  end
end

#send!Object



77
78
79
80
81
# File 'app/models/discuss/message.rb', line 77

def send!
  lock.synchronize do
    Discuss::MessageSender.new(self).run unless draft?
  end
end

#senderObject



46
47
48
49
50
51
52
# File 'app/models/discuss/message.rb', line 46

def sender
  if sent?
    user
  else
    parent ? parent.user : user
  end
end

#sent_dateObject



132
133
134
# File 'app/models/discuss/message.rb', line 132

def sent_date
  sent_at || received_at
end

#trash!Object



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

def trash!
  update(trashed_at: Time.zone.now)
end

#uneditable?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/discuss/message.rb', line 119

def uneditable?
  !editable?
end

#unread?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/discuss/message.rb', line 115

def unread?
  !read?
end

#unsent?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'app/models/discuss/message.rb', line 123

def unsent?
  !sent?
end