2016-11-17 04:04:30 +00:00
|
|
|
class NotificationsController < ApplicationController
|
2016-11-17 04:50:47 +00:00
|
|
|
before_action :set_receipts, only: [:index, :show, :mark_read, :mark_unread]
|
|
|
|
before_action :set_notification, only: [:show, :mark_read, :mark_unread]
|
|
|
|
before_action :set_receipt, only: [:show, :mark_read, :mark_unread]
|
|
|
|
|
2016-11-17 04:04:30 +00:00
|
|
|
def index
|
|
|
|
@notifications = current_user.mailbox.notifications
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2016-11-17 04:50:47 +00:00
|
|
|
format.json do
|
|
|
|
render json: @notifications.map do |notification|
|
|
|
|
receipt = @receipts.find_by(notification_id: notification.id)
|
|
|
|
notification.as_json.merge(is_read: receipt.is_read)
|
|
|
|
end
|
|
|
|
end
|
2016-11-17 04:04:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2016-11-17 04:50:47 +00:00
|
|
|
@receipt.update(is_read: true)
|
2016-11-17 04:04:30 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
2016-11-17 04:50:47 +00:00
|
|
|
format.json do
|
|
|
|
render json: @notification.as_json.merge(
|
|
|
|
is_read: @receipt.is_read
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_read
|
|
|
|
@receipt.update(is_read: true)
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
format.json do
|
|
|
|
render json: @notification.as_json.merge(
|
|
|
|
is_read: @receipt.is_read
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_unread
|
|
|
|
@receipt.update(is_read: false)
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
format.json do
|
|
|
|
render json: @notification.as_json.merge(
|
|
|
|
is_read: @receipt.is_read
|
|
|
|
)
|
|
|
|
end
|
2016-11-17 04:04:30 +00:00
|
|
|
end
|
|
|
|
end
|
2016-11-17 04:50:47 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_receipts
|
2016-11-24 16:07:57 +00:00
|
|
|
@receipts = current_user.mailboxer_notification_receipts
|
2016-11-17 04:50:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_notification
|
|
|
|
@notification = current_user.mailbox.notifications.find_by(id: params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_receipt
|
|
|
|
@receipt = @receipts.find_by(notification_id: params[:id])
|
|
|
|
end
|
|
|
|
|
2016-11-17 04:04:30 +00:00
|
|
|
end
|