Number of unread notifications

I use the public_activity gem to create a notification list for users. Using this post as a link, I try to save the number of notifications that the user has not read. When the user clicks to see the notifications that they have, I want the account to return to zero. The solution to the above question is to create a class method as follows:

def self.unread
where(:read => false)
end

and then put this in the view:

user.notifications.unread.update_all(:read => true)

My controller is as follows:

def notifications
@activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user.id)
end

def self.unread
where(:read => false)
end

and my view is as follows:

<% @activities.each do |activity| %>    
<%= render_activity activity %> 
<% end %>

My question is: where to add:

.update_all(:read => true)

in the view and how can I get unread.count.

+4
source share
1 answer

. ajax, .

, , facebook.

def notifications
   @activities = PublicActivity::Activity.order("created_at desc").where(recipient_id: current_user.id)
   @notification_count = @activities.where(:read => false).count  
end

def read_all_notification
   PublicActivity::Activity.where(recipient_id: current_user.id).update_all(:read => true)
end 

2

get 'some_controller/notifications'
post 'some_controller/read_all_notification'

, render_activity , action ajax. , , some_id id. ,

$(document).on 'click' , '#some_id' , (e)->
    e.preventDefault()
    $.ajax '/some_controller/read_all_notification' ,
        type: "post"
        dataType: "json"
        beforeSend: (xhr) ->
          xhr.setRequestHeader "X-CSRF-Token", $("meta[name=\"csrf-token\"]").attr("content")
        cache: false

.

, render_activity , how what, .

https://github.com/pokonski/public_activity/blob/master/lib/public_activity/renderable.rb#L16-L143

render_activity.

.

+5

All Articles