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

Is it possible to use the gem Unread with the gem Public Activity?

我目前正在使用 gem 'Public Activity',在视图中我有一个用户 activity 被过滤以仅向该用户显示适用于他们的活动,例如 'John Smith commented on your post'。但是,我想为此添加通知,例如 facebook 或 twitter,其中徽章显示数字,当您看到提要时徽章消失。

我找到了一个名为 Unread 的 gem,它看起来很理想,除了它需要将 acts_as_readable :on => :created_at 添加到您的模型中,并且因为我正在使用 public_activity gem class 无法添加此内容。

是否可以将未读 gem 所需的代码注入 PublicActivity:Activity class?

链接:

gem public_activity: https://github.com/pokonski/public_activity

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

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

    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

实际上可以像这样向 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 和 Public Activity 的人提供了一个片段,这样您就可以拥有一个 activity_policy.rb 策略文件。

注意:请务必使用包含此行的最新版本 Public Activity:https://github.com/chaps-io/public_activity/blob/1-5-stable/lib/public_activity/orm/active_record/activity.rb#L9 因为没有设置 base_class,Unread 将使用错误的多态class姓名。