Filter notifications by target type

This commit is contained in:
Karina Kwiatek 2022-07-21 16:30:04 +02:00 committed by Karina Kwiatek
parent f475cd0a0b
commit 0992d44aef
2 changed files with 13 additions and 10 deletions

View File

@ -4,8 +4,11 @@ class NotificationsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
TYPE_MAPPINGS = { TYPE_MAPPINGS = {
"smile" => "appendable::reaction", "answer" => Notification::QuestionAnswered.name,
"reaction" => "appendable::reaction" "comment" => Notification::Commented.name,
"commentsmile" => Notification::CommentSmiled.name,
"relationship" => Notification::StartedFollowing.name,
"smile" => Notification::Smiled.name,
}.freeze }.freeze
def index def index

View File

@ -13,7 +13,7 @@ class Notification < ApplicationRecord
end end
def for_type(recipient, type, options={}) def for_type(recipient, type, options={})
self.where(options.merge!(recipient: recipient)).where('LOWER(target_type) = ?', type).order(:created_at).reverse_order self.where(options.merge!(recipient: recipient)).where(type: type).order(:created_at).reverse_order
end end
def notify(recipient, target) def notify(recipient, target)
@ -38,12 +38,12 @@ class Notification < ApplicationRecord
private private
def make_notification(recipient, target, notification_type) def make_notification(recipient, target, notification_type)
n = notification_type.new(target: target, n = notification_type.new(target: target,
recipient: recipient, recipient: recipient,
new: true) new: true)
n.save! n.save!
n n
end end
end end
end end