Fix number of new notifications

We were displaying the total number of notifications with a message "You
have N unread notifications", but were using the total number of
notifications instead of the unread ones.
This commit is contained in:
Javi Martín
2021-02-16 01:24:33 +01:00
parent 0839c5ea75
commit fb88e0b77c
3 changed files with 11 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ class Layout::NotificationItemComponent < ApplicationComponent
def text def text
if unread_notifications? if unread_notifications?
t("layouts.header.notification_item.new_notifications", count: user.notifications_count) t("layouts.header.notification_item.new_notifications", count: unread_notifications.count)
else else
t("layouts.header.notification_item.no_notifications") t("layouts.header.notification_item.no_notifications")
end end
@@ -24,6 +24,10 @@ class Layout::NotificationItemComponent < ApplicationComponent
end end
def unread_notifications? def unread_notifications?
user.notifications.unread.count > 0 unread_notifications.count > 0
end
def unread_notifications
user.notifications.unread
end end
end end

View File

@@ -2,6 +2,8 @@ shared_examples "notifiable in-app" do |factory_name|
let(:author) { create(:user, :verified) } let(:author) { create(:user, :verified) }
let!(:notifiable) { create(factory_name, author: author) } let!(:notifiable) { create(factory_name, author: author) }
before { create(:notification, :read, notifiable: notifiable, user: author) }
scenario "Notification message is shown" do scenario "Notification message is shown" do
create(:notification, notifiable: notifiable, user: author) create(:notification, notifiable: notifiable, user: author)

View File

@@ -198,7 +198,7 @@ describe "Proposal Notifications" do
login_as user1 login_as user1
visit root_path visit root_path
find(".unread-notifications").click click_link "You have a new notification"
expect(page).to have_css ".notification", count: 1 expect(page).to have_css ".notification", count: 1
@@ -210,7 +210,7 @@ describe "Proposal Notifications" do
login_as user2 login_as user2
visit root_path visit root_path
find(".unread-notifications").click click_link "You have a new notification"
expect(page).to have_css ".notification", count: 1 expect(page).to have_css ".notification", count: 1
@@ -303,7 +303,7 @@ describe "Proposal Notifications" do
login_as user login_as user
visit root_path visit root_path
find(".unread-notifications").click click_link "You have a new notification"
expect(page).to have_css ".notification", count: 1 expect(page).to have_css ".notification", count: 1
expect(page).to have_content "This resource is not available anymore" expect(page).to have_content "This resource is not available anymore"