From 3e3501d201ee4edc6dfa82b26a681d8109b9cb16 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Fri, 27 Jan 2023 20:31:38 +0100 Subject: [PATCH] inbox: update inbox entries in controller --- app/controllers/inbox_controller.rb | 7 +++++++ app/views/layouts/inbox.html.haml | 1 - spec/controllers/inbox_controller_spec.rb | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/inbox_controller.rb b/app/controllers/inbox_controller.rb index 19c6a70d..48cc5675 100644 --- a/app/controllers/inbox_controller.rb +++ b/app/controllers/inbox_controller.rb @@ -3,6 +3,8 @@ class InboxController < ApplicationController before_action :authenticate_user! + after_action :mark_inbox_entries_as_read, only: %i[show] + def show find_author find_inbox_entries @@ -77,4 +79,9 @@ class InboxController < ApplicationController .joins(:question) .where(questions: { user: @author_user, author_is_anonymous: false }) end + + def mark_inbox_entries_as_read + # using .dup to not modify @inbox -- useful in tests + @inbox&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations + end end diff --git a/app/views/layouts/inbox.html.haml b/app/views/layouts/inbox.html.haml index b54b82b5..cb931773 100644 --- a/app/views/layouts/inbox.html.haml +++ b/app/views/layouts/inbox.html.haml @@ -9,6 +9,5 @@ = render 'shared/links' :ruby - @inbox.update_all(new: false) provide(:title, generate_title('Inbox')) parent_layout 'base' diff --git a/spec/controllers/inbox_controller_spec.rb b/spec/controllers/inbox_controller_spec.rb index 6cb2e128..bc0ba2ad 100644 --- a/spec/controllers/inbox_controller_spec.rb +++ b/spec/controllers/inbox_controller_spec.rb @@ -58,6 +58,10 @@ describe InboxController, type: :controller do end end + it "updates the inbox entry status" do + expect { subject }.to change { inbox_entry.reload.new? }.from(true).to(false) + end + context "when requested the turbo stream format" do subject { get :show, format: :turbo_stream }