Update caching timestamps in model events

This commit is contained in:
Karina Kwiatek 2023-06-16 18:18:40 +02:00
parent 1b05063f4a
commit ece64669a1
3 changed files with 32 additions and 3 deletions

View File

@ -13,6 +13,18 @@ class Inbox < ApplicationRecord
!user.privacy_allow_anonymous_questions?
end
after_create do
user.touch(:inbox_updated_at)
end
after_update do
user.touch(:inbox_updated_at)
end
after_destroy do
user.touch(:inbox_updated_at)
end
def answer(answer_content, user)
raise Errors::AnsweringOtherBlockedSelf if question.user&.blocking?(user)
raise Errors::AnsweringSelfBlockedOther if user.blocking?(question.user)

View File

@ -1,9 +1,21 @@
# frozen_string_literal: true
class Notification < ApplicationRecord
belongs_to :recipient, class_name: "User", touch: :notifications_updated_at
belongs_to :recipient, class_name: "User"
belongs_to :target, polymorphic: true
after_create do
recipient.touch(:notifications_updated_at)
end
after_update do
recipient.touch(:notifications_updated_at)
end
after_destroy do
recipient.touch(:notifications_updated_at)
end
class << self
include CursorPaginatable
@ -45,7 +57,7 @@ class Notification < ApplicationRecord
n = notification_type.new(target:,
recipient:,
new: true)
new: true,)
n.save!
n
end

View File

@ -39,7 +39,12 @@ class Subscription < ApplicationRecord
{ target_id: source.id, target_type: Comment, recipient_id: s.user_id, new: true, type: Notification::Commented, created_at: source.created_at, updated_at: source.created_at }
end
Notification.insert_all!(notifications) unless notifications.empty? # rubocop:disable Rails/SkipsModelValidations
return if notifications.empty?
# rubocop:disable Rails/SkipsModelValidations
Notification.insert_all!(notifications)
User.where(id: notifications.pluck(:recipient_id)).touch_all(:notifications_updated_at)
# rubocop:enable Rails/SkipsModelValidations
end
def denotify(source, target)