Use one controller for all notification readings

This commit is contained in:
David Heinemeier Hansson
2025-04-15 19:14:50 +02:00
parent 2ea898324f
commit 73f272f132
9 changed files with 30 additions and 51 deletions
@@ -1,18 +0,0 @@
class Notifications::MarkAllReadsController < ApplicationController
def create
Current.user.notifications.unread.read_all
set_page_and_extract_portion_from Current.user.notifications.read.ordered if request.format.turbo_stream?
Turbo::StreamsChannel.broadcast_update_to(
[ Current.user, :notifications ],
target: "notifications",
html: ""
)
respond_to do |format|
format.turbo_stream
format.html
end
end
end
@@ -0,0 +1,14 @@
class Notifications::ReadingsController < ApplicationController
def create
@notification = Current.user.notifications.find(params[:id])
@notification.update!(read_at: Time.current)
end
def create_all
Current.user.notifications.unread.read_all
set_page_and_extract_portion_from Current.user.notifications.read.ordered if request.format.turbo_stream?
Turbo::StreamsChannel.broadcast_update_to [ Current.user, :notifications ], target: "notifications", html: ""
end
end
@@ -6,14 +6,4 @@ class NotificationsController < ApplicationController
@unread = Current.user.notifications.unread.ordered
end
end
def mark_read
@notification = Current.user.notifications.find(params[:id])
@notification.update!(read_at: Time.current)
respond_to do |format|
format.html { redirect_back fallback_location: notifications_path }
format.turbo_stream
end
end
end
+1 -1
View File
@@ -32,7 +32,7 @@ module NotificationsHelper
end
def notification_mark_read_button(notification)
button_to mark_read_notification_path(notification),
button_to read_notification_path(notification),
class: "notification__unread_indicator btn borderless",
title: "Mark as read",
data: { turbo_frame: "_top" } do
+4 -4
View File
@@ -6,7 +6,7 @@
dialog_modal_value: false,
dialog_target: "dialog",
action: "keydown.esc->dialog#close:stop click@document->dialog#closeOnClickOutside" } do %>
<%= turbo_frame_tag("notifications", src: notifications_tray_path) -%>
<%= turbo_frame_tag("notifications", src: tray_notifications_path) -%>
<%= link_to notifications_path, class: "tray__overflow border-radius txt-normal", data: { action: "click->dialog#close"} do %>
<div class="notification__content border-radius shadow fill-white flex align-center txt-align-center gap full-width border txt-ink txt-small">
@@ -17,9 +17,9 @@
<% end %>
<div class="tray__actions notification-tray__actions border border-radius">
<%= button_to notifications_mark_all_read_path,
class: "notification-tray__read_action btn btn--reversed fill-transparent borderless flex-item-justify-start shadow txt-xx-small",
data: { action: "click->dialog#close", turbo_frame: "notifications" } do %>
<%= button_to read_all_notifications_path,
class: "notification-tray__read_action btn btn--reversed fill-transparent borderless flex-item-justify-start shadow txt-xx-small",
data: { action: "click->dialog#close", turbo_frame: "notifications" } do %>
<%= icon_tag "check" %>
<span class="for-screen-reader">Mark all read</span>
<% end %>
+3 -6
View File
@@ -6,9 +6,8 @@
<h1 class="txt-x-large"><%= @page_title %></h1>
<%= link_to notifications_settings_path, class: "btn flex-item-justify-end" do %>
<%= icon_tag "settings" %>
<span class="for-screen-reader">Notification settings</span>
<%= link_to settings_notifications_path, class: "btn flex-item-justify-end" do %>
<%= icon_tag "settings" %> <span class="for-screen-reader">Notification settings</span>
<% end %>
</nav>
<% end %>
@@ -18,9 +17,7 @@
<% if @unread.any? %>
<div class="flex align-center justify-space-between margin-block-start margin-block-end-half">
<h2 class="txt-medium txt-uppercase txt-alert">New for you</h2>
<%= button_to notifications_mark_all_read_path, class: "btn txt-small" do %>
Mark all as read
<% end %>
<%= button_to "Mark all as read", read_all_notifications_path, class: "btn txt-small" %>
</div>
<%= render @unread %>
+7 -11
View File
@@ -46,20 +46,16 @@ Rails.application.routes.draw do
end
resources :notifications, only: :index
namespace :notifications do
resource :tray, only: :show
resource :mark_all_read, only: :create
resources :mark_read, only: :create
resource :settings, only: :show
end
resources :notifications do
member do
post :mark_read
scope module: :notifications do
get "tray", to: "trays#show", on: :collection
get "settings", to: "settings#show", on: :collection
post "readings", to: "readings#create_all", on: :collection, as: :read_all
post "reading", to: "readings#create", on: :member, as: :read
end
end
resources :filters
resources :events, only: :index
@@ -8,7 +8,7 @@ class Notifications::MarkAllReadsControllerTest < ActionDispatch::IntegrationTes
test "show" do
assert_changes -> { notifications(:logo_published_kevin).reload.read? }, from: false, to: true do
assert_changes -> { notifications(:layout_commented_kevin).reload.read? }, from: false, to: true do
post notifications_mark_all_read_path
post read_all_notifications_path
end
end