comment.rb
766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Comment
include Mongoid::Document
include Mongoid::Timestamps
after_create :increase_counter_cache
before_destroy :decrease_counter_cache
after_create :deliver_email, if: :emailable?
field :body, type: String
index(user_id: 1)
belongs_to :err, class_name: "Problem"
belongs_to :user
delegate :app, to: :err
validates :body, presence: true
def deliver_email
Mailer.comment_notification(self).deliver_now
end
def notification_recipients
app.notification_recipients - [user.email]
end
def emailable?
app.emailable? && notification_recipients.any?
end
protected
def increase_counter_cache
err.inc(comments_count: 1)
end
def decrease_counter_cache
err.inc(comments_count: -1) if err
end
end