diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 98d96d34..0516063c 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -15,10 +15,15 @@ class NotificationsController < ApplicationController def mark_as_read current_user.notifications.unread.update_all(read_at: Time.zone.now) - redirect_to notifications_url, notice: 'All notifications marked as read.', status: :see_other end + + def destroy_all + current_user.notifications.destroy_all + redirect_to notifications_url, notice: 'All notifications where successfully destroyed.', status: :see_other + end + def destroy @notification.destroy! redirect_to notifications_url, notice: 'Notification was successfully destroyed.', status: :see_other diff --git a/app/views/notifications/index.html.erb b/app/views/notifications/index.html.erb index 03fd4acc..09e760fb 100644 --- a/app/views/notifications/index.html.erb +++ b/app/views/notifications/index.html.erb @@ -4,7 +4,10 @@

Notifications

<% if @notifications.unread.any? %> - <%= link_to "Mark all as read", mark_notifications_as_read_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-primary" %> + <%= link_to "Mark all as read", mark_notifications_as_read_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-primary" %>  + <% end %> + <% if @notifications.any? %> + <%= link_to "Delete all", delete_all_notifications_path, method: :post, data: { turbo_method: :post }, class: "btn btn-sm btn-warning" %> <% end %>
diff --git a/config/routes.rb b/config/routes.rb index 2c40e93d..02f70e8c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -38,6 +38,7 @@ Rails.application.routes.draw do end resources :notifications, only: %i[index show destroy] post 'notifications/mark_as_read', to: 'notifications#mark_as_read', as: :mark_notifications_as_read + post 'notifications/destroy_all', to: 'notifications#destroy_all', as: :delete_all_notifications resources :stats, only: :index do collection do post :update