Retrospring/app/models/answer.rb

43 lines
1.2 KiB
Ruby
Raw Normal View History

2014-10-27 22:36:38 -07:00
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
2014-11-30 10:43:22 -08:00
has_many :comments, dependent: :destroy
has_many :smiles, dependent: :destroy
2015-04-20 18:12:11 -07:00
has_many :subscriptions, dependent: :destroy
2014-12-14 05:34:51 -08:00
after_create do
Inbox.where(user: self.user, question: self.question).destroy_all
Notification.notify self.question.user, self unless self.question.author_is_anonymous
2015-04-20 18:12:11 -07:00
Subscription.subscribe self.user, self
2015-04-21 07:53:59 -07:00
Subscription.subscribe self.question.user, self unless self.question.user.nil?
self.user.increment! :answered_count
self.question.increment! :answer_count
end
2014-12-28 12:34:42 -08:00
before_destroy do
# mark a report as deleted if it exists
rep = Report.where(target_id: self.id).first
unless rep.nil?
rep.deleted = true
rep.save
end
2014-12-28 12:20:07 -08: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-20 18:12:11 -07:00
Subscription.denotify comment, self
2014-12-28 12:20:07 -08:00
end
Notification.denotify self.question.user, self
2015-04-20 18:12:11 -07:00
Subscription.destruct self
2014-12-28 12:34:42 -08:00
end
def notification_type(*_args)
Notifications::QuestionAnswered
2014-12-28 12:20:07 -08:00
end
2014-10-27 22:36:38 -07:00
end