2020-04-19 00:59:18 +02:00
|
|
|
class Answer < ApplicationRecord
|
2020-04-20 23:03:57 +02:00
|
|
|
extend Answer::TimelineMethods
|
|
|
|
|
2014-10-28 06:36:38 +01:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :question
|
2014-11-30 19:43:22 +01:00
|
|
|
has_many :comments, dependent: :destroy
|
|
|
|
has_many :smiles, dependent: :destroy
|
2015-04-21 03:12:11 +02:00
|
|
|
has_many :subscriptions, dependent: :destroy
|
2015-05-04 23:06:57 +02:00
|
|
|
has_many :comment_smiles, through: :comments, source: :smiles
|
2014-12-14 14:34:51 +01:00
|
|
|
|
2015-01-03 18:09:56 +01:00
|
|
|
after_create do
|
2015-01-03 19:02:56 +01:00
|
|
|
Inbox.where(user: self.user, question: self.question).destroy_all
|
|
|
|
|
2015-04-22 22:37:50 +02:00
|
|
|
Notification.notify self.question.user, self unless self.question.user == self.user or self.question.user.nil?
|
2015-04-21 03:12:11 +02:00
|
|
|
Subscription.subscribe self.user, self
|
2015-04-22 22:37:50 +02:00
|
|
|
Subscription.subscribe self.question.user, self unless self.question.author_is_anonymous
|
2015-01-03 18:09:56 +01:00
|
|
|
self.user.increment! :answered_count
|
|
|
|
self.question.increment! :answer_count
|
|
|
|
end
|
|
|
|
|
2014-12-28 21:34:42 +01:00
|
|
|
before_destroy do
|
|
|
|
# mark a report as deleted if it exists
|
2015-04-30 02:22:24 +02:00
|
|
|
rep = Report.where(target_id: self.id, type: 'Reports::Answer')
|
2015-04-30 02:04:43 +02:00
|
|
|
rep.each do |r|
|
|
|
|
unless r.nil?
|
|
|
|
r.deleted = true
|
|
|
|
r.save
|
|
|
|
end
|
2014-12-28 21:34:42 +01:00
|
|
|
end
|
2014-12-28 21:20:07 +01:00
|
|
|
|
|
|
|
self.user.decrement! :answered_count
|
|
|
|
self.question.decrement! :answer_count
|
|
|
|
self.smiles.each do |smile|
|
|
|
|
Notification.denotify self.user, smile
|
|
|
|
end
|
|
|
|
self.comments.each do |comment|
|
|
|
|
comment.user.decrement! :commented_count
|
2015-04-21 03:12:11 +02:00
|
|
|
Subscription.denotify comment, self
|
2014-12-28 21:20:07 +01:00
|
|
|
end
|
|
|
|
Notification.denotify self.question.user, self
|
2015-04-21 03:12:11 +02:00
|
|
|
Subscription.destruct self
|
2014-12-28 21:34:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def notification_type(*_args)
|
|
|
|
Notifications::QuestionAnswered
|
2014-12-28 21:20:07 +01:00
|
|
|
end
|
2014-10-28 06:36:38 +01:00
|
|
|
end
|