Skip to content

Commit 1e102bc

Browse files
committed
feature/Create notifications index page
1 parent 87227c7 commit 1e102bc

File tree

8 files changed

+40
-1
lines changed

8 files changed

+40
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class NotificationsController < ApplicationController
2+
after_action -> { current_user.mark_notifications_as_read }, only: :index
3+
4+
def index
5+
@page_title = 'Notifications'
6+
@notifications = current_user.notifications.order(created_at: :desc)
7+
end
8+
end

app/helpers/application_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@ def notification_counter(user)
1616
"<span class='menu-button--notification'>#{count_for_display}</span>".html_safe
1717
end
1818
end
19+
20+
def notification_for_display(notification)
21+
case notification.notifiable_type
22+
when 'Follow'
23+
render partial: 'notifications/follow', locals: { notification: notification }
24+
end
25+
end
1926
end

app/models/notification.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ class Notification < ApplicationRecord
33
belongs_to :notifiable, polymorphic: true
44

55
scope :unread, -> { where(read: false) }
6+
7+
def mark_as_read
8+
update(read: true)
9+
end
610
end

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ def avatar_url
8080
end
8181
end
8282

83+
def mark_notifications_as_read
84+
notifications.unread.each { |notification| notification.mark_as_read }
85+
end
86+
8387
def follow(user)
8488
follow = Follow.create(followed_user_id: user.id, follower_id: id)
8589
follow.notifications.create(user: user)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<% follower = notification.notifiable.follower %>
2+
3+
<div
4+
class="card--container card--container-padding"
5+
style="cursor: pointer;"
6+
data-controller="navigation"
7+
data-navigation-url-value="<%= user_path(follower) %>"
8+
data-action="click->navigation#navigate"
9+
>
10+
<span><%= follower.name %> followed you</span>
11+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<% @notifications.each do |notification| %>
2+
<%= notification_for_display(notification) %>
3+
<% end %>

app/views/shared/_sidebar.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<% end %>
1414
</div>
1515
<div class="menu-button--wrapper">
16-
<%= link_to folders_path, class: request.fullpath == folders_path ? 'menu-button--item-active' : 'menu-button--item' do %>
16+
<%= link_to notifications_path, class: request.fullpath == notifications_path ? 'menu-button--item-active' : 'menu-button--item' do %>
1717
<%= notification_counter(current_user) %>
1818
<img src="/icons/icons8-notification-24.png" width="22">
1919
<span class="menu-button--name">Notifications</span>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
get :popover, on: :member
5555
end
5656

57+
resources :notifications, only: :index
58+
5759
resources :users, only: %i(index show) do
5860
resources :snippets, only: :index
5961

0 commit comments

Comments
 (0)