dawarich/app/controllers/notifications_controller.rb

38 lines
1 KiB
Ruby
Raw Permalink Normal View History

2024-07-04 16:20:12 -04:00
# frozen_string_literal: true
class NotificationsController < ApplicationController
before_action :authenticate_user!
before_action :set_notification, only: %i[show destroy]
def index
2024-07-04 17:00:07 -04:00
@notifications =
2024-07-24 14:25:16 -04:00
current_user.notifications.order(created_at: :desc).page(params[:page]).per(20)
2024-07-04 16:20:12 -04:00
end
2024-07-09 14:09:43 -04:00
def show
@notification.update!(read_at: Time.zone.now) unless @notification.read_at?
end
2024-07-04 16:20:12 -04:00
2024-07-04 17:00:07 -04:00
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
2024-12-15 11:28:59 -05:00
def destroy_all
current_user.notifications.destroy_all
2024-12-15 11:28:59 -05:00
redirect_to notifications_url, notice: 'All notifications where successfully destroyed.', status: :see_other
end
2024-07-04 16:20:12 -04:00
def destroy
@notification.destroy!
redirect_to notifications_url, notice: 'Notification was successfully destroyed.', status: :see_other
end
private
def set_notification
@notification = current_user.notifications.find(params[:id])
2024-07-04 16:20:12 -04:00
end
end