Move category and high_priority to payload classes

Use polymorphism instead of case statements in Native push target:
- DefaultPayload#category returns "default", #high_priority? returns false
- EventPayload#category returns "assignment"/"comment"/"card" based on action
- MentionPayload#category returns "mention", #high_priority? returns true

This simplifies the Native push target by delegating source-specific
logic to the appropriate payload classes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Rosa Gutierrez
2026-01-22 13:19:18 +01:00
parent 9299300dbf
commit 4f19c42958
5 changed files with 43 additions and 50 deletions
@@ -23,6 +23,14 @@ class Notification::DefaultPayload
notifications_url
end
def category
"default"
end
def high_priority?
false
end
private
def card_url(card)
Rails.application.routes.url_helpers.card_url(card, **url_options)
+12
View File
@@ -36,6 +36,18 @@ class Notification::EventPayload < Notification::DefaultPayload
end
end
def category
case event.action
when "card_assigned" then "assignment"
when "comment_created" then "comment"
else "card"
end
end
def high_priority?
event.action.card_assigned?
end
private
def event
notification.source
@@ -13,6 +13,14 @@ class Notification::MentionPayload < Notification::DefaultPayload
card_url(card)
end
def category
"mention"
end
def high_priority?
true
end
private
def mention
notification.source
@@ -24,7 +24,7 @@ class Notification::PushTarget::Native < Notification::PushTarget
ApplicationPushNotification
.with_apple(
aps: {
category: notification_category,
category: payload.category,
"mutable-content": 1,
"interruption-level": interruption_level
}
@@ -41,7 +41,7 @@ class Notification::PushTarget::Native < Notification::PushTarget
card_id: card&.id,
card_title: card&.title,
creator_name: notification.creator.name,
category: notification_category
category: payload.category
)
.new(
title: payload.title,
@@ -49,39 +49,17 @@ class Notification::PushTarget::Native < Notification::PushTarget
badge: notification.user.notifications.unread.count,
sound: "default",
thread_id: card&.id,
high_priority: high_priority_notification?
high_priority: payload.high_priority?
)
end
def notification_category
case notification.source
when Event
case notification.source.action
when "card_assigned" then "assignment"
when "comment_created" then "comment"
else "card"
end
when Mention
"mention"
else
"default"
end
end
def interruption_level
high_priority_notification? ? "time-sensitive" : "active"
end
def high_priority_notification?
case notification.source
when Event then notification.source.action.card_assigned?
when Mention then true
else false
end
payload.high_priority? ? "time-sensitive" : "active"
end
def creator_avatar_url
return unless notification.creator.respond_to?(:avatar) && notification.creator.avatar.attached?
if notification.creator.respond_to?(:avatar) && notification.creator.avatar.attached?
Rails.application.routes.url_helpers.url_for(notification.creator.avatar)
end
end
end
@@ -10,39 +10,26 @@ class Notification::PushTarget::NativeTest < ActiveSupport::TestCase
@user.push_subscriptions.delete_all
end
test "notification_category returns assignment for card_assigned" do
test "payload category returns assignment for card_assigned" do
notification = notifications(:logo_assignment_kevin)
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
assert_equal "assignment", push.send(:notification_category)
assert_equal "assignment", notification.payload.category
end
test "notification_category returns comment for comment_created" do
test "payload category returns comment for comment_created" do
notification = notifications(:layout_commented_kevin)
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
assert_equal "comment", push.send(:notification_category)
assert_equal "comment", notification.payload.category
end
test "notification_category returns mention for mentions" do
test "payload category returns mention for mentions" do
notification = notifications(:logo_card_david_mention_by_jz)
notification.user.identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(notification)
assert_equal "mention", push.send(:notification_category)
assert_equal "mention", notification.payload.category
end
test "notification_category returns card for other card events" do
@identity.devices.create!(token: "test123", platform: "apple", name: "Test iPhone")
push = Notification::PushTarget::Native.new(@notification)
assert_equal "card", push.send(:notification_category)
test "payload category returns card for other card events" do
assert_equal "card", @notification.payload.category
end