-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathcomment.rb
More file actions
38 lines (32 loc) · 1.01 KB
/
comment.rb
File metadata and controls
38 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
class Comment < ApplicationRecord
belongs_to :author, class_name: 'Member', inverse_of: :comments
belongs_to :commentable, polymorphic: true, counter_cache: true
# validates :body, presence: true
validate :author_is_not_blocked
scope :post_order, -> { order(created_at: :asc) } # for display on post page
after_create do
recipient = commentable.author.id
sender = author.id
# don't send notifications to yourself
if recipient != sender
Notification.create(
recipient_id: recipient,
sender_id: sender,
subject: "#{author} commented on #{commentable.subject}",
body:,
notifiable: commentable
)
end
end
def to_s
"#{author.login_name} commented on #{commentable.subject}"
end
private
def author_is_not_blocked
return unless author
if commentable.author.already_blocking?(author)
errors.add(:base, "You cannot comment on a post of a member who has blocked you.")
end
end
end