Touch `notifications_updated_at` after marking entries as read

This commit is contained in:
Karina Kwiatek 2023-05-07 20:54:54 +02:00
parent ed6bdfe8cb
commit 2aaba3e2d9
2 changed files with 14 additions and 1 deletions

View File

@ -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

View File

@ -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