Retrospring/app/models/answer.rb

52 lines
1.6 KiB
Ruby
Raw Normal View History

2020-04-18 15:59:18 -07:00
class Answer < ApplicationRecord
extend Answer::TimelineMethods
2014-10-27 22:36:38 -07:00
belongs_to :user
belongs_to :question
2014-11-30 10:43:22 -08:00
has_many :comments, dependent: :destroy
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
2015-04-20 18:12:11 -07:00
has_many :subscriptions, dependent: :destroy
2015-05-04 14:06:57 -07:00
has_many :comment_smiles, through: :comments, source: :smiles
2014-12-14 05:34:51 -08:00
validates :question_id, uniqueness: { scope: :user_id }
# rubocop:disable Rails/SkipsModelValidations
after_create do
Inbox.where(user: self.user, question: self.question).destroy_all
Notification.notify self.question.user, self unless self.question.user == self.user or self.question.user.nil?
2015-04-20 18:12:11 -07:00
Subscription.subscribe self.user, self
Subscription.subscribe self.question.user, self unless self.question.author_is_anonymous
user.increment! :answered_count
question.increment! :answer_count
end
2014-12-28 12:34:42 -08:00
before_destroy do
# mark a report as deleted if it exists
2015-04-29 17:22:24 -07:00
rep = Report.where(target_id: self.id, type: 'Reports::Answer')
2015-04-29 17:04:43 -07:00
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
2014-12-28 12:34:42 -08:00
end
2014-12-28 12:20:07 -08:00
user&.decrement! :answered_count
question&.decrement! :answer_count
2014-12-28 12:20:07 -08:00
self.smiles.each do |smile|
Notification.denotify self.user, smile
end
self.comments.each do |comment|
comment.user&.decrement! :commented_count
2015-04-20 18:12:11 -07:00
Subscription.denotify comment, self
2014-12-28 12:20:07 -08:00
end
Notification.denotify question&.user, self
2015-04-20 18:12:11 -07:00
Subscription.destruct self
2014-12-28 12:34:42 -08:00
end
# rubocop:enable Rails/SkipsModelValidations
2014-12-28 12:34:42 -08:00
def notification_type(*_args)
Notification::QuestionAnswered
2014-12-28 12:20:07 -08:00
end
2014-10-27 22:36:38 -07:00
end