From 2aaba3e2d90589ad4f6a8404cb26c7ae339ec3c0 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sun, 7 May 2023 20:54:54 +0200 Subject: [PATCH] Touch `notifications_updated_at` after marking entries as read --- app/controllers/notifications_controller.rb | 5 ++++- spec/controllers/notifications_controller_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 8338047f..defe6410 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -48,10 +48,13 @@ class NotificationsController < ApplicationController .count(:target_type) end + # rubocop:disable Rails/SkipsModelValidations def mark_notifications_as_read # using .dup to not modify @notifications -- useful in tests - @notifications&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations + @notifications&.dup&.update_all(new: false) + current_user.touch(:notifications_updated_at) end + # rubocop:enable Rails/SkipsModelValidations def cursored_notifications_for(type:, last_id:, size: nil) cursor_params = { last_id: last_id, size: size }.compact diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index 0ba10a12..85241645 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -3,9 +3,12 @@ require "rails_helper" describe NotificationsController do + include ActiveSupport::Testing::TimeHelpers + describe "#index" do subject { get :index, params: { type: :new } } + let(:original_notifications_updated_at) { 1.day.ago } let(:user) { FactoryBot.create(:user) } before do @@ -38,6 +41,13 @@ describe NotificationsController do it "marks notifications as read" do expect { subject }.to change { Notification.for(user).where(new: true).count }.from(2).to(0) end + + it "updates the the timestamp used for caching" do + user.update(notifications_updated_at: original_notifications_updated_at) + travel 1.second do + expect { subject }.to change { user.reload.notifications_updated_at }.from(original_notifications_updated_at).to(Time.now.utc) + end + end end end