Retrospring/app/models/comment.rb

37 lines
963 B
Ruby
Raw Normal View History

2020-04-18 15:59:18 -07:00
class Comment < ApplicationRecord
2014-10-27 22:36:38 -07:00
belongs_to :user
belongs_to :answer
2014-11-30 10:43:03 -08:00
validates :user_id, presence: true
validates :answer_id, presence: true
has_many :smiles, class_name: "Appendable::Reaction", foreign_key: :parent_id, dependent: :destroy
2014-12-07 05:29:47 -08:00
validates :content, length: { maximum: 160 }
2014-12-14 06:10:23 -08:00
# rubocop:disable Rails/SkipsModelValidations
2014-12-28 12:58:11 -08:00
after_create do
Subscription.subscribe self.user, answer, false
2015-04-20 18:12:11 -07:00
Subscription.notify self, answer
2014-12-28 12:58:11 -08:00
user.increment! :commented_count
answer.increment! :comment_count
end
before_destroy do
2015-04-29 17:22:24 -07:00
rep = Report.where(target_id: self.id, type: 'Reports::Comment')
2015-04-29 17:04:43 -07:00
rep.each do |r|
unless r.nil?
r.deleted = true
r.save
end
end
2015-04-20 18:12:11 -07:00
Subscription.denotify self, answer
user&.decrement! :commented_count
answer&.decrement! :comment_count
2014-12-28 12:58:11 -08:00
end
# rubocop:enable Rails/SkipsModelValidations
2014-12-28 12:58:11 -08:00
2014-12-14 06:10:23 -08:00
def notification_type(*_args)
Notification::Commented
2014-12-14 06:10:23 -08:00
end
2014-10-27 22:36:38 -07:00
end