Class: NotificationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/notifications_controller.rb

Overview

Handles in-app notifications for station events.

Instance Method Summary collapse

Methods inherited from ApplicationController

#honeypot

Instance Method Details

#destroyObject

DESTROY /notifications/:id



41
42
43
44
45
46
# File 'app/controllers/notifications_controller.rb', line 41

def destroy
  @notification = @user.notifications.find(params[:id])
  @notification.destroy
  flash[:success] = 'Notification deleted.'
  redirect_to action: :index
end

#destroy_allObject

DESTROY /notifications



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/notifications_controller.rb', line 49

def destroy_all
  @notifications = @user.notifications
  @notifications = @notifications.where(read: true) if params[:condition] == 'read'
  # Use time input to limit chonographically
  if (!params[:time].nil? && !params[:time_unit].nil?)
    time = params[:time].to_i
    unit = ['days', 'weeks', 'months', 'years'].include?(params[:time_unit]) ?  \
        params[:time_unit].to_sym : nil
    @notifications = @notifications.where('created_at <= ?', time.send(unit).ago)
  end

  if @notifications.destroy_all.size.nonzero?
    flash[:success] = 'All notifications have been deleted.'
  else
    flash[:failed] = 'No notifications to delete.'
  end

  redirect_to action: :index
end

#indexObject

Display notifications belonging to the currently logged in user. GET /notifications



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/notifications_controller.rb', line 11

def index
  @notifications = @user.notifications
          .order(created_at: :desc)
          .paginate(page: params[:page])

  @title = "Inbox"
  @title += "(#{@unread_notifications_count})" unless @unread_notifications_count.nil?

  # Render response before marking notifications as read
  respond_to do |format|
    format.html { render :index }
  end
  # Mark notifications as read after page has been rendered
  @notifications.update_all(read: true) if @notifications.present?
end

#update_allObject

PATCH /notifications



28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/notifications_controller.rb', line 28

def update_all
  @notifications = @user.notifications.where(read: false)
  if @notifications.update_all(read: true) > 0
    flash[:success] = 'All notifications have been marked as read.'
    redirect_to action: :index
  else
    flash[:error] = 'No unread notifications found.'
    redirect_to action: :index
  end

end