diff --git a/app/models/user/inbox_methods.rb b/app/models/user/inbox_methods.rb index 15276579..8e3b00fe 100644 --- a/app/models/user/inbox_methods.rb +++ b/app/models/user/inbox_methods.rb @@ -14,19 +14,16 @@ module User::InboxMethods end def unread_inbox_count - Rails.cache.fetch("#{inbox_cache_key}/unread_inbox_count") do - count = Inbox.select("COUNT(id) AS count") - .where(new: true) - .where(user_id: id) - .group(:user_id) - .order(:count) - .first - return nil if count.nil? - return nil unless count.count.positive? + Rails.cache.fetch(inbox_cache_key) do + count = Inbox.where(new: true, user_id: id).count(:id) - count.count + # Returning +nil+ here in order to not display a counter + # at all when there isn't anything in the user's inbox + return nil unless count.positive? + + count end end - def inbox_cache_key = "#{cache_key}-#{inbox_updated_at}" + def inbox_cache_key = "#{cache_key}/unread_inbox_count-#{inbox_updated_at}" end diff --git a/app/models/user/notification_methods.rb b/app/models/user/notification_methods.rb index d123f121..db7e5018 100644 --- a/app/models/user/notification_methods.rb +++ b/app/models/user/notification_methods.rb @@ -2,13 +2,16 @@ module User::NotificationMethods def unread_notification_count - Rails.cache.fetch("#{notification_cache_key}/unread_notification_count") do - count = Notification.for(self).where(new: true).count + Rails.cache.fetch(notification_cache_key) do + count = Notification.for(self).where(new: true).count(:id) + + # Returning +nil+ here in order to not display a counter + # at all when there aren't any notifications return nil unless count.positive? count end end - def notification_cache_key = "#{cache_key}-#{notifications_updated_at}" + def notification_cache_key = "#{cache_key}/unread_notification_count-#{notifications_updated_at}" end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index abec3188..15d7601b 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -3,50 +3,6 @@ require "rails_helper" describe ApplicationHelper, type: :helper do - describe "#inbox_count" do - context "no signed in user" do - it "should return 0 as inbox count" do - expect(helper.inbox_count).to eq(0) - end - end - - context "user is signed in" do - let(:user) { FactoryBot.create(:user) } - let(:question) { FactoryBot.create(:question) } - - before do - sign_in(user) - Inbox.create(user_id: user.id, question_id: question.id, new: true) - end - - it "should return the inbox count" do - expect(helper.inbox_count).to eq(1) - end - end - end - - describe "#notification_count" do - context "no signed in user" do - it "should return 0 as notification count" do - expect(helper.notification_count).to eq(0) - end - end - - context "user is signed in" do - let(:user) { FactoryBot.create(:user) } - let(:another_user) { FactoryBot.create(:user) } - - before do - sign_in(user) - another_user.follow(user) - end - - it "should return the notification count" do - expect(helper.notification_count).to eq(1) - end - end - end - describe "#privileged" do context "current user and checked user do not match" do let(:user) { FactoryBot.create(:user) } diff --git a/spec/models/user/inbox_methods_spec.rb b/spec/models/user/inbox_methods_spec.rb new file mode 100644 index 00000000..efa7d16e --- /dev/null +++ b/spec/models/user/inbox_methods_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe User::InboxMethods do + context "given a user" do + let(:user) { FactoryBot.create(:user) } + + describe "#unread_inbox_count" do + subject { user.unread_inbox_count } + + context "user has no questions in their inbox" do + it "should return nil" do + expect(subject).to eq(nil) + end + end + + context "user has 1 question in their inbox" do + # FactoryBot seems to have issues with setting the +new+ field on inbox entries + # so we can create it manually instead + let!(:inbox) { Inbox.create(question: FactoryBot.create(:question), user:, new: true) } + + it "should return 1" do + expect(subject).to eq(1) + end + end + end + end +end diff --git a/spec/models/user/notification_methods_spec.rb b/spec/models/user/notification_methods_spec.rb new file mode 100644 index 00000000..550d3116 --- /dev/null +++ b/spec/models/user/notification_methods_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe User::NotificationMethods do + context "given a user" do + let(:user) { FactoryBot.create(:user) } + + describe "#unread_notification_count" do + subject { user.unread_notification_count } + + context "user has no notifications" do + it "should return nil" do + expect(subject).to eq(nil) + end + end + + context "user has a notification" do + let(:other_user) { FactoryBot.create(:user) } + + before do + other_user.follow(user) + end + + it "should return 1" do + expect(subject).to eq(1) + end + end + end + end +end