2022-02-19 08:19:28 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-25 20:54:48 -07:00
|
|
|
class Reaction < ApplicationRecord
|
|
|
|
belongs_to :parent, polymorphic: true
|
|
|
|
belongs_to :user
|
|
|
|
|
2022-03-26 10:42:49 -07:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2022-02-27 06:43:19 -08:00
|
|
|
after_create do
|
|
|
|
Notification.notify parent.user, self unless parent.user == user
|
|
|
|
user.increment! :smiled_count
|
|
|
|
parent.increment! :smile_count
|
|
|
|
end
|
|
|
|
|
|
|
|
before_destroy do
|
2022-07-17 12:03:23 -07:00
|
|
|
Notification.denotify parent&.user, self
|
2022-02-04 13:08:25 -08:00
|
|
|
user&.decrement! :smiled_count
|
|
|
|
parent&.decrement! :smile_count
|
2022-02-27 06:43:19 -08:00
|
|
|
end
|
2022-03-26 10:42:49 -07:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2022-02-27 06:43:19 -08:00
|
|
|
|
|
|
|
def notification_type(*_args)
|
2022-07-21 09:22:03 -07:00
|
|
|
return Notification::CommentSmiled if parent.instance_of?(Comment)
|
|
|
|
|
2022-07-20 12:43:50 -07:00
|
|
|
Notification::Smiled
|
2022-02-27 06:43:19 -08:00
|
|
|
end
|
2022-02-16 14:42:00 -08:00
|
|
|
end
|