Delegate target to the notifiable objects

This commit is contained in:
Jorge Manrubia
2025-04-23 16:15:08 +02:00
parent 5dac14ab50
commit f258b05b23
7 changed files with 17 additions and 15 deletions
+4
View File
@@ -11,6 +11,10 @@ module Notifiable
Notifier.for(self)&.notify
end
def notifiable_target
self
end
private
def notify_recipients_later
NotifyRecipientsJob.perform_later self
+1 -1
View File
@@ -16,7 +16,7 @@ class Event < ApplicationRecord
super.inquiry
end
def target
def notifiable_target
if action.commented?
comment
else
+4
View File
@@ -11,6 +11,10 @@ class Mention < ApplicationRecord
mentioner == mentionee
end
def notifiable_target
source
end
private
def add_mentionee_as_watcher
source.watch_by(mentionee)
+2 -8
View File
@@ -9,6 +9,8 @@ class Notification < ApplicationRecord
after_create_commit :broadcast_unread
delegate :notifiable_target, to: :source
def self.read_all
update!(read_at: Time.current)
end
@@ -21,14 +23,6 @@ class Notification < ApplicationRecord
read_at.present?
end
def target
if source.is_a?(Event)
source.target
else
source
end
end
private
def broadcast_unread
broadcast_prepend_later_to user, :notifications, target: "notifications"
+4 -4
View File
@@ -4,10 +4,10 @@ class Notifier
class << self
def for(source)
case source
when ::Event
Notifier::Event.new(source)
when ::Mention
Notifier::Mention.new(source)
when Event
EventNotifier.new(source)
when Mention
MentionNotifier.new(source)
end
end
end
+1 -1
View File
@@ -108,7 +108,7 @@ Rails.application.routes.draw do
end
resolve "Notification" do |notification, options|
polymorphic_path(notification.target, options)
polymorphic_path(notification.notifiable_target, options)
end
get "up", to: "rails/health#show", as: :rails_health_check
+1 -1
View File
@@ -2,7 +2,7 @@ require "test_helper"
class NotifierTest < ActiveSupport::TestCase
test "for returns the matching notifier class for the event" do
assert_kind_of Notifier::Event, Notifier.for(events(:logo_published))
assert_kind_of Notifier::EventNotifier, Notifier.for(events(:logo_published))
end
test "generate does not create notifications if the event was system-generated" do