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)


39
40
41
# File 'app/models/discuss/message.rb', line 39

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

#delete!Object



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

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

#draft?Boolean

passed in from the compose form

Returns:

  • (Boolean)


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

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

#mailboxObject



59
60
61
62
63
64
65
66
67
68
# File 'app/models/discuss/message.rb', line 59

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



90
91
92
# File 'app/models/discuss/message.rb', line 90

def read!
  update(read_at: Time.zone.now)
end

#receive!Object



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

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

#recipient_listObject



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

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

#recipientsObject



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

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

#recipients=(users) ⇒ Object



51
52
53
# File 'app/models/discuss/message.rb', line 51

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

#reply!(options = {}) ⇒ Object



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

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



70
71
72
73
74
# File 'app/models/discuss/message.rb', line 70

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

#senderObject



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

def sender
  sent? ? user : parent.user
end

#sent_dateObject



121
122
123
# File 'app/models/discuss/message.rb', line 121

def sent_date
  sent_at || received_at
end

#trash!Object



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

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

#uneditable?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/models/discuss/message.rb', line 108

def uneditable?
  !editable?
end

#unsent?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/discuss/message.rb', line 112

def unsent?
  !sent?
end