Skip to content

Commit 87227c7

Browse files
committed
feature/Create notifications and associate with follows
1 parent b09b28c commit 87227c7

File tree

9 files changed

+64
-2
lines changed

9 files changed

+64
-2
lines changed

app/assets/stylesheets/_sidebar.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.menu-button {
1818
&--wrapper {
1919
display: flex;
20+
position: relative;
2021
}
2122

2223
&--icon {
@@ -27,6 +28,18 @@
2728
margin-left: 14px;
2829
}
2930

31+
&--notification {
32+
position: absolute;
33+
top: 18px;
34+
left: 55px;
35+
width: 14px;
36+
height: 14px;
37+
background-color: $color-purple;
38+
border-radius: 50%;
39+
font-size: 12px;
40+
color: white;
41+
}
42+
3043
&--item {
3144
flex: 1;
3245
display: flex;

app/helpers/application_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ def tabs_class_for_active_tab(tab_id, param_tab_id)
66
def tabs_details_class_for_active_tab(tab_id, param_tab_id)
77
"hidden" unless tab_id.to_sym == param_tab_id.to_sym
88
end
9+
10+
def notification_counter(user)
11+
count = current_user.notifications.unread.size
12+
13+
if count > 0
14+
count_for_display = count > 9 ? 9 : count
15+
16+
"<span class='menu-button--notification'>#{count_for_display}</span>".html_safe
17+
end
18+
end
919
end

app/models/follow.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Follow < ApplicationRecord
2+
has_many :notifications, as: :notifiable, dependent: :destroy
23
# The user being followed
34
belongs_to :followed_user, foreign_key: :followed_user_id, class_name: 'User'
45

app/models/notification.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Notification < ApplicationRecord
2+
belongs_to :user
3+
belongs_to :notifiable, polymorphic: true
4+
5+
scope :unread, -> { where(read: false) }
6+
end

app/models/user.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class User < ApplicationRecord
1111
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
1212
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :confirmable, :validatable
1313

14+
has_many :notifications
1415
has_many :folders
1516
has_many :snippet_folders, through: :folders
1617

@@ -80,7 +81,8 @@ def avatar_url
8081
end
8182

8283
def follow(user)
83-
Follow.create(followed_user_id: user.id, follower_id: id)
84+
follow = Follow.create(followed_user_id: user.id, follower_id: id)
85+
follow.notifications.create(user: user)
8486
end
8587

8688
def unfollow(user)

app/views/shared/_sidebar.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<span class="menu-button--name">Home</span>
1313
<% end %>
1414
</div>
15+
<div class="menu-button--wrapper">
16+
<%= link_to folders_path, class: request.fullpath == folders_path ? 'menu-button--item-active' : 'menu-button--item' do %>
17+
<%= notification_counter(current_user) %>
18+
<img src="/icons/icons8-notification-24.png" width="22">
19+
<span class="menu-button--name">Notifications</span>
20+
<% end %>
21+
</div>
1522
<div class="menu-button--wrapper">
1623
<%= link_to folders_path, class: request.fullpath == folders_path ? 'menu-button--item-active' : 'menu-button--item' do %>
1724
<img src="/icons/icons8-folder.svg" width="22">
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateNotifications < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :notifications do |t|
4+
t.references :user, null: false, foreign_key: true
5+
t.references :notifiable, polymorphic: true, null: false
6+
t.boolean :read, null: false, default: false
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_12_30_115654) do
13+
ActiveRecord::Schema.define(version: 2020_12_31_171047) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -71,6 +71,17 @@
7171
t.index ["user_id"], name: "index_likes_on_user_id"
7272
end
7373

74+
create_table "notifications", force: :cascade do |t|
75+
t.bigint "user_id", null: false
76+
t.string "notifiable_type", null: false
77+
t.bigint "notifiable_id", null: false
78+
t.boolean "read", default: false, null: false
79+
t.datetime "created_at", precision: 6, null: false
80+
t.datetime "updated_at", precision: 6, null: false
81+
t.index ["notifiable_type", "notifiable_id"], name: "index_notifications_on_notifiable_type_and_notifiable_id"
82+
t.index ["user_id"], name: "index_notifications_on_user_id"
83+
end
84+
7485
create_table "snippet_folders", force: :cascade do |t|
7586
t.bigint "snippet_id", null: false
7687
t.bigint "folder_id", null: false
@@ -118,6 +129,7 @@
118129
add_foreign_key "comments", "users"
119130
add_foreign_key "likes", "snippets"
120131
add_foreign_key "likes", "users"
132+
add_foreign_key "notifications", "users"
121133
add_foreign_key "snippet_folders", "folders"
122134
add_foreign_key "snippet_folders", "snippets"
123135
end
447 Bytes
Loading

0 commit comments

Comments
 (0)