From 699f9c8144dc866efb7ea26c4b472c87cfad7461 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Mon, 1 Jun 2020 09:27:46 -0500 Subject: [PATCH] [deploy] Optimization:Use Scope and current_user to Get Notification Count (#8177) --- .../notifications/counts_controller.rb | 2 +- app/models/notification.rb | 1 + app/services/get_unseen_notifications_service.rb | 12 ------------ spec/requests/notification_counts_spec.rb | 15 +++++++++++++-- .../notifications/notifications_page_spec.rb | 7 +++++-- 5 files changed, 20 insertions(+), 17 deletions(-) delete mode 100644 app/services/get_unseen_notifications_service.rb diff --git a/app/controllers/notifications/counts_controller.rb b/app/controllers/notifications/counts_controller.rb index 41acab1db..161b49328 100644 --- a/app/controllers/notifications/counts_controller.rb +++ b/app/controllers/notifications/counts_controller.rb @@ -1,6 +1,6 @@ class Notifications::CountsController < ApplicationController def index - count = GetUnseenNotificationsService.new(current_user).get + count = current_user ? current_user.notifications.unread.count : 0 render plain: count.to_s end end diff --git a/app/models/notification.rb b/app/models/notification.rb index fe75ddf21..b297764f9 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -21,6 +21,7 @@ class Notification < ApplicationRecord scope :for_organization_mentions, lambda { |org_id| where(organization_id: org_id, notifiable_type: "Mention", user_id: nil) } + scope :unread, -> { where(read: false) } class << self def send_new_follower_notification(follow, is_read = false) diff --git a/app/services/get_unseen_notifications_service.rb b/app/services/get_unseen_notifications_service.rb deleted file mode 100644 index 9be1946c1..000000000 --- a/app/services/get_unseen_notifications_service.rb +++ /dev/null @@ -1,12 +0,0 @@ -class GetUnseenNotificationsService - def initialize(user) - @user = user - end - - def get - return 1 if Rails.env.test? - return 1 unless @user - - NotificationCounter.new(@user).unread_notification_count - end -end diff --git a/spec/requests/notification_counts_spec.rb b/spec/requests/notification_counts_spec.rb index 8899b3ad3..76b441bae 100644 --- a/spec/requests/notification_counts_spec.rb +++ b/spec/requests/notification_counts_spec.rb @@ -1,11 +1,22 @@ require "rails_helper" RSpec.describe "NotificationCounts", type: :request do + let(:user) { create(:user) } + let(:following_user) { create(:user) } + describe "GET /notifications/counts" do - it "returns a number" do - # stubbed to be 1 + it "returns count if signed in" do + sign_in user + follow_instance = following_user.follow(user) + Notification.send_new_follower_notification_without_delay(follow_instance) + get "/notifications/counts" expect(response.body).to eq("1") end + + it "returns 0 if no user is present" do + get "/notifications/counts" + expect(response.body).to eq("0") + end end end diff --git a/spec/system/notifications/notifications_page_spec.rb b/spec/system/notifications/notifications_page_spec.rb index 020f825c9..632a5625a 100644 --- a/spec/system/notifications/notifications_page_spec.rb +++ b/spec/system/notifications/notifications_page_spec.rb @@ -1,8 +1,8 @@ require "rails_helper" RSpec.describe "Notifications page", type: :system, js: true do - let_it_be(:alex) { create(:user) } - let_it_be(:leslie) { create(:user) } + let(:alex) { create(:user) } + let(:leslie) { create(:user) } before { sign_in alex } @@ -15,6 +15,9 @@ RSpec.describe "Notifications page", type: :system, js: true do end it "shows 1 notification and disappear after clicking it" do + follow_instance = leslie.follow(alex) + Notification.send_new_follower_notification_without_delay(follow_instance) + visit "/" expect(page).to have_css("span#notifications-number", text: "1") click_link("notifications-link")