是否可以将gem Unread与gem Public Activity一起使用?

时间:2015-03-25 18:41:50

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

我目前正在使用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

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

2 个答案:

答案 0 :(得分:5)

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

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

然后,每当我有用户评论或类似内容时,我在控制器中构建了一个新通知,传递以下信息:

    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

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

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

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

然后,在通知控制器中,我让它在视图上运行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

答案 1 :(得分:1)

实际上可以向PublicActivity::Class添加额外的内容,如下所示:

将名为public_activity.rb的此文件添加到config/initializers

PublicActivity::Activity.class_eval do
  acts_as_readable :on => :created_at

  def self.policy_class
    ActivityPolicy
  end
end

我还为那些使用Pundit公共活动的人提供了一个片段,这样你就可以拥有一个activity_policy.rb政策文件。

注意:请务必使用包含以下行的最新版本的公共活动:https://github.com/chaps-io/public_activity/blob/1-5-stable/lib/public_activity/orm/active_record/activity.rb#L9,因为未设置base_class,未读将使用错误的多态类名称。