abb*_*567 1 ruby notifications ruby-on-rails read-unread public-activity
我目前正在使用gem'公共活动',并且在视图中我将用户活动过滤为仅向用户显示适用于他们的活动,例如"John Smith在您的帖子上发表评论".但是,我想在此添加通知,例如facebook或twitter,其中徽章显示一个数字,当您看到Feed时,徽章就会消失.
我找到了一个名为Unread的宝石看起来很理想,除了它需要添加acts_as_readable :on => :created_at到你的模型,并且因为我使用的是public_activity gem,所以无法添加这个类.
是否可以将未读的gem所需的代码注入PublicActivity:Activity类?
链接:
gem public_activity:https://github.com/pokonski/public_activity
对于将来可能会发现这一点的任何人,我通过数据建模我自己的通知实现了一个完全不同的系统.我没有使用公共活动和未读,而是制作了一个名为通知的新模型.这有列:
recipient:integer
sender:integer
post_id:integer
type:string
read:boolean
Run Code Online (Sandbox Code Playgroud)
然后,每当我有用户评论或类似内容时,我在控制器中构建了一个新通知,传递以下信息:
recipient = @post.user.id
sender = current_user.id
post_id = @post.id
type = "comment" # like, or comment etc
read = false # read is false by default
Run Code Online (Sandbox Code Playgroud)
在导航栏中,我只是添加了一个徽章,该徽章根据应用程序控制器变量计算current_users未读通知.
@unreadnotifications = current_user.notifications.where(read: false)
<% if @unreadnotifications.count != 0 %>
(<%= @unreadnotifications.count %>)
<% end %>
Run Code Online (Sandbox Code Playgroud)
然后,在通知控制器中,我让它mark_as_read在视图上运行一个动作.然后将通知计数设置回0.
before_action :mark_as_read
def mark_as_read
@unread = current_user.notifications.where(read: false)
@unread.each do |unread|
unread.update_attribute(:read, true)
end
end
Run Code Online (Sandbox Code Playgroud)