[deploy] Optimization:Use Scope and current_user to Get Notification Count (#8177)
This commit is contained in:
parent
a3c62df266
commit
699f9c8144
5 changed files with 20 additions and 17 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue