Class: NotificationsController

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

Overview

Provides web and API actions relating to user notifications.

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
# File 'app/controllers/notifications_controller.rb', line 5

def index
  @notifications = Notification.unscoped.where(user: current_user).paginate(page: params[:page], per_page: 100)
                               .order(Arel.sql('is_read ASC, created_at DESC'))
  respond_to do |format|
    format.html { render :index, layout: 'without_sidebar' }
    format.json { render json: @notifications, methods: :community_name }
  end
end

#readObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/notifications_controller.rb', line 14

def read
  @notification = Notification.unscoped.find params[:id]

  unless @notification.user == current_user
    respond_to do |format|
      format.html { render template: 'errors/forbidden', status: :forbidden }
      format.json { render json: nil, status: :forbidden }
    end
    return
  end

  @notification.is_read = !@notification.is_read
  if @notification.save
    respond_to do |format|
      format.html do
        flash[:notice] = 'Marked as read.'
        render :index
      end
      format.json { render json: { status: 'success', notification: @notification }, methods: :community_name }
    end
  else
    respond_to do |format|
      format.html do
        flash[:error] = 'Failed to mark read.'
        render :index
      end
      format.json { render json: { status: 'failed' } }
    end
  end
end

#read_allObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/notifications_controller.rb', line 45

def read_all
  @notifications = Notification.unscoped.where(user: current_user, is_read: false)
  if @notifications.update_all(is_read: true)
    respond_to do |format|
      format.html do
        flash[:notice] = 'Marked all as read.'
        render :index
      end
      format.json { render json: { status: 'success' } }
    end
  else
    respond_to do |format|
      format.html do
        flash[:error] = 'Failed to mark all read.'
        render :index
      end
      format.json { render json: { status: 'failed' } }
    end
  end
end