From f258b05b239f8b8e095ca141c61dfb707e31b4b6 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Wed, 23 Apr 2025 16:15:08 +0200 Subject: [PATCH] Delegate target to the notifiable objects --- app/models/concerns/notifiable.rb | 4 ++++ app/models/event.rb | 2 +- app/models/mention.rb | 4 ++++ app/models/notification.rb | 10 ++-------- app/models/notifier.rb | 8 ++++---- config/routes.rb | 2 +- test/models/notifier_test.rb | 2 +- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/models/concerns/notifiable.rb b/app/models/concerns/notifiable.rb index 7c3531ed2..4723229d9 100644 --- a/app/models/concerns/notifiable.rb +++ b/app/models/concerns/notifiable.rb @@ -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 diff --git a/app/models/event.rb b/app/models/event.rb index 7fa70bd77..0fe643bb5 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -16,7 +16,7 @@ class Event < ApplicationRecord super.inquiry end - def target + def notifiable_target if action.commented? comment else diff --git a/app/models/mention.rb b/app/models/mention.rb index cbf4ad1b3..c14237976 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -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) diff --git a/app/models/notification.rb b/app/models/notification.rb index 35c2ac702..01444ce61 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -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" diff --git a/app/models/notifier.rb b/app/models/notifier.rb index 08ea8c9ad..548b3869c 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 5ba46a8f8..0acad2014 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/models/notifier_test.rb b/test/models/notifier_test.rb index 8dc9c8afe..d27379464 100644 --- a/test/models/notifier_test.rb +++ b/test/models/notifier_test.rb @@ -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