Skip to content

Commit 385c6ed

Browse files
committed
feature/Create notification for likes
1 parent 1d3a440 commit 385c6ed

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

app/models/like.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
class Like < ApplicationRecord
22
belongs_to :snippet, counter_cache: true
33
belongs_to :user
4+
has_many :notifications, as: :notifiable, dependent: :destroy
45

56
def self.toggle(user_id, snippet_id)
67
like = Like.find_or_initialize_by(user_id: user_id, snippet_id: snippet_id)
7-
like.persisted? ? like.destroy : like.tap(&:save!)
8+
9+
if like.persisted?
10+
like.destroy
11+
else
12+
like.save!
13+
14+
unless user_id.to_i == like.snippet.user_id
15+
like.notifications.create!(user_id: like.snippet.user_id)
16+
end
17+
end
818
end
919
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<% liker = notification.notifiable.user %>
2+
<% snippet = notification.notifiable.snippet %>
3+
4+
<%= render 'notifications/card', { user: liker, icon: 'chat', url: user_path(liker) } do %>
5+
<div class="min-w-0 flex-1">
6+
<div>
7+
<div class="text-sm">
8+
<%= link_to liker.name, user_path(liker), class: "font-medium text-gray-900" %>
9+
</div>
10+
<p class="mt-0.5 text-sm text-gray-500">
11+
Liked <%= link_to snippet.description, snippet_path(snippet), class: "font-medium text-gray-800" %> <%= time_ago_in_words(notification.created_at) %> ago
12+
</p>
13+
</div>
14+
</div>
15+
<% end %>

0 commit comments

Comments
 (0)