Files
fizzy/app/helpers/notifications_helper.rb
T
Mike Dalessio 3156fbd770 Fix notification rendering on card closure events
where the closure no longer exists, possibly due to being
reopened.

Previously, fragment caching generally prevented any problems, but if
the cache is busted for any reason (like Andy did in c7eeb56c), then
the previous logic would raise an exception like "undefined method
'reason' for nil".

So, if the closure no longer exists, let's just note that it was
closed, and by who.
2025-06-30 23:45:53 -04:00

73 lines
2.3 KiB
Ruby

module NotificationsHelper
def event_notification_title(event)
case event_notification_action(event)
when "comment_created" then "RE: #{card_notification_title(event.eventable.card)}"
else card_notification_title(event.eventable)
end
end
def event_notification_body(event)
name = event.creator.name
case event_notification_action(event)
when "card_closed"
if event.card.closure
"Closed as “#{event.card.closure.reason}” by #{name}"
else
"Closed by #{name}"
end
when "card_reopened" then "Reopened by #{name}"
when "card_published" then "Added by #{name}"
when "comment_created" then comment_notification_body(event)
when "card_assigned" then "Assigned to #{event.assignees.none? ? "self" : event.assignees.pluck(:name).to_sentence}"
else name
end
end
def notification_tag(notification, &)
tag.div id: dom_id(notification), class: "tray__item" do
concat(
link_to(notification,
class: [ "card card--notification", { "card--closed": notification.card.closed? } ],
data: { turbo_frame: "_top" },
style: { "--card-color:": notification.card.color },
&)
)
end
end
def notification_mark_read_button(notification)
button_to read_notification_path(notification),
class: "card__notification-unread-indicator btn btn--circle borderless",
title: "Mark as read",
data: { action: "form#submit:stop", controller: "form", form_target: "submit", turbo_frame: "_top" } do
concat(icon_tag("remove-med"))
concat(tag.span("Mark as read", class: "for-screen-reader"))
end
end
def notifications_next_page_link(page)
unless @page.last?
tag.div id: "next_page", data: { controller: "fetch-on-visible", fetch_on_visible_url_value: notifications_path(page: @page.next_param) }
end
end
private
def event_notification_action(event)
if event.action.card_published? && event.eventable.assigned_to?(event.creator)
"card_assigned"
else
event.action
end
end
def comment_notification_body(event)
comment = event.eventable
strip_tags(comment.body.to_s).truncate(200)
end
def card_notification_title(card)
card.title.presence || "Card #{card.id}"
end
end