Retrospring/app/models/reaction.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

29 lines
723 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class Reaction < ApplicationRecord
belongs_to :parent, polymorphic: true
belongs_to :user
validates :parent_id, uniqueness: { scope: :user_id }
2024-03-19 14:03:28 -07:00
2022-03-26 10:42:49 -07:00
# rubocop:disable Rails/SkipsModelValidations
after_create do
Notification.notify parent.user, self unless parent.user == user
user.increment! :smiled_count
parent.increment! :smile_count
end
before_destroy do
Notification.denotify parent&.user, self
user&.decrement! :smiled_count
parent&.decrement! :smile_count
end
2022-03-26 10:42:49 -07:00
# rubocop:enable Rails/SkipsModelValidations
def notification_type(*_args)
return Notification::CommentSmiled if parent.instance_of?(Comment)
Notification::Smiled
end
end