ruby-on-rails - 是否可以将 gem Unread 与 gem Public Activity 一起使用?

标签 ruby-on-rails ruby notifications public-activity read-unread

我目前正在使用 gem“公共(public)事件”,在 View 中我过滤了一个用户事件以仅向该用户显示适用于他们的事件,例如“John Smith 评论了您的帖子”。但是,我想为此添加通知,例如 facebook 或 twitter,其中徽章显示数字,当您看到提要时徽章消失。

我找到了一个名为 Unread 的 gem,它看起来很理想,除了它需要将 acts_as_readable :on => :created_at 添加到您的模型中,并且由于我使用的是 public_activity gem,所以该类不可访问添加这个。

是否可以将未读 gem 所需的代码注入(inject)到 PublicActivity:Activity 类中?

链接:

gem 公共(public)事件:https://github.com/pokonski/public_activity

gem 未读:https://github.com/ledermann/unread

最佳答案

对于将来可能会发现这一点的任何人,我通过对自己的通知进行数据建模来实现一个完全不同的系统。我没有使用公共(public)事件和未读,而是创建了一个名为通知的新模型。这有列:

    recipient:integer
    sender:integer
    post_id:integer
    type:string
    read:boolean

然后,每当我有用户评论或点赞等时,我都会在 Controller 中构建一个新通知,传递以下信息:

    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

在导航栏中,我只是添加了一个徽章,用于根据应用程序 Controller 变量计算 current_users 未读通知。

    @unreadnotifications = current_user.notifications.where(read: false)

    <% if @unreadnotifications.count != 0 %>
        (<%= @unreadnotifications.count %>)
    <% end %>

然后,在通知 Controller 中,我让它在 View 上运行 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

关于ruby-on-rails - 是否可以将 gem Unread 与 gem Public Activity 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29263920/

相关文章:

Ruby Net::HTTP 使用 https 时获取请求错误

c++ - 如果我们使用 notify_one() 来唤醒线程,我们还需要 yield() - 在 C++ 中吗?

php - 我们如何使用 Firebase 云消息传递 (fcm) 在 android 中发送图像和图标以进行推送通知

ruby-on-rails - ActiveRecord 中属性的 Attr_encrypted 顺序

css - 基础 gem 不兼容单位 : 'rem' and 'px'

ruby - 从命令行运行 Ruby 库

ruby-on-rails - RSpec.configure 和请求对象

mysql - 使用 redis 将所有用户通知保存在列表中?

ruby-on-rails - 为什么 bundler 无法访问 http ://rubygems. 组织?

ruby-on-rails - 如何在 Electron 中正确运行 Rails 应用程序?