ability to mark as read/unread

This commit is contained in:
Devin Howard 2016-11-16 23:50:47 -05:00
parent 7dd37c50d3
commit dcb70e86e2
5 changed files with 76 additions and 5 deletions

View file

@ -1,19 +1,71 @@
class NotificationsController < ApplicationController
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]
def index
@notifications = current_user.mailbox.notifications
respond_to do |format|
format.html
format.json { render json: @notifications.to_json }
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
end
end
def show
@notification = current_user.mailbox.notifications.find_by(id: params[:id])
@receipt.update(is_read: true)
respond_to do |format|
format.html
format.json { render json: @notification.to_json }
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
end
end
private
def set_receipts
@receipts = current_user.mailbox.receipts
.includes(:notification).where(mailbox_type: nil)
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
end

View file

@ -6,11 +6,19 @@
<h4>Notifications</h4>
<ul>
<% @notifications.each do |notification| %>
<% receipt = @receipts.find_by(notification_id: notification.id) %>
<li>
<strong>
<%= link_to notification.subject, notification_path(notification.id) %>
</strong>
<%= notification.body.truncate(140) %>
<span id="notification-<%= notification.id %>" class="read-unread">
<% if receipt.is_read? %>
<%= link_to '(read)', mark_unread_notification_path(notification.id), remote: true, method: :put %>
<% else %>
<%= link_to '(unread)', mark_read_notification_path(notification.id), remote: true, method: :put %>
<% end %>
</span>
</li>
<% end %>
</ul>

View file

@ -0,0 +1,3 @@
$('#notification-<%= @notification.id %> > a')
.text('(read)')
.attr('href', '<%= mark_unread_notification_path(@notification.id) %>')

View file

@ -0,0 +1,3 @@
$('#notification-<%= @notification.id %> > a')
.text('(unread)')
.attr('href', '<%= mark_read_notification_path(@notification.id) %>')

View file

@ -36,7 +36,12 @@ Metamaps::Application.routes.draw do
resources :mappings, except: [:index, :new, :edit]
resources :messages, only: [:show, :create, :update, :destroy]
resources :notifications, only: [:index, :show]
resources :notifications, only: [:index, :show] do
member do
put :mark_read
put :mark_unread
end
end
resources :metacode_sets, except: [:show]