Touch `inbox_updated_at` after marking entries as read

This commit is contained in:
Karina Kwiatek 2023-05-07 20:39:09 +02:00
parent 578dd9c6e6
commit ed6bdfe8cb
2 changed files with 16 additions and 3 deletions

View File

@ -82,10 +82,13 @@ class InboxController < ApplicationController
.where(questions: { user: @author_user, author_is_anonymous: false }) .where(questions: { user: @author_user, author_is_anonymous: false })
end end
# rubocop:disable Rails/SkipsModelValidations
def mark_inbox_entries_as_read def mark_inbox_entries_as_read
# using .dup to not modify @inbox -- useful in tests # using .dup to not modify @inbox -- useful in tests
@inbox&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations @inbox&.dup&.update_all(new: false)
current_user.touch(:inbox_updated_at)
end end
# rubocop:enable Rails/SkipsModelValidations
def increment_metric def increment_metric
Retrospring::Metrics::QUESTIONS_ASKED.increment( Retrospring::Metrics::QUESTIONS_ASKED.increment(

View File

@ -3,7 +3,10 @@
require "rails_helper" require "rails_helper"
describe InboxController, type: :controller do describe InboxController, type: :controller do
let(:user) { FactoryBot.create(:user) } include ActiveSupport::Testing::TimeHelpers
let(:original_inbox_updated_at) { 1.day.ago }
let(:user) { FactoryBot.create(:user, inbox_updated_at: original_inbox_updated_at) }
describe "#show" do describe "#show" do
shared_examples_for "sets the expected ivars" do shared_examples_for "sets the expected ivars" do
@ -53,7 +56,7 @@ describe InboxController, type: :controller do
more_data_available: false, more_data_available: false,
inbox_count: 1, inbox_count: 1,
delete_id: "ib-delete-all", delete_id: "ib-delete-all",
disabled: nil disabled: nil,
} }
end end
end end
@ -62,6 +65,13 @@ describe InboxController, type: :controller do
expect { subject }.to change { inbox_entry.reload.new? }.from(true).to(false) expect { subject }.to change { inbox_entry.reload.new? }.from(true).to(false)
end end
it "updates the the timestamp used for caching" do
user.update(inbox_updated_at: original_inbox_updated_at)
travel 1.second do
expect { subject }.to change { user.reload.inbox_updated_at }.from(original_inbox_updated_at).to(Time.now.utc)
end
end
context "when requested the turbo stream format" do context "when requested the turbo stream format" do
subject { get :show, format: :turbo_stream } subject { get :show, format: :turbo_stream }