2022-02-19 17:19:28 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-10-26 05:54:48 +02:00
|
|
|
class Reaction < ApplicationRecord
|
|
|
|
belongs_to :parent, polymorphic: true
|
|
|
|
belongs_to :user
|
|
|
|
|
2022-03-26 18:42:49 +01:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
2022-02-27 15:43:19 +01: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 21:03:23 +02:00
|
|
|
Notification.denotify parent&.user, self
|
2022-02-04 22:08:25 +01:00
|
|
|
user&.decrement! :smiled_count
|
|
|
|
parent&.decrement! :smile_count
|
2022-02-27 15:43:19 +01:00
|
|
|
end
|
2022-03-26 18:42:49 +01:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
2022-02-27 15:43:19 +01:00
|
|
|
|
|
|
|
def notification_type(*_args)
|
2022-07-21 18:22:03 +02:00
|
|
|
return Notification::CommentSmiled if parent.instance_of?(Comment)
|
|
|
|
|
2022-07-20 21:43:50 +02:00
|
|
|
Notification::Smiled
|
2022-02-27 15:43:19 +01:00
|
|
|
end
|
2022-02-16 23:42:00 +01:00
|
|
|
end
|