如何创建一个简单的活动流?

发布于 2024-12-08 20:56:02 字数 1033 浏览 0 评论 0原文

我正在尝试实现一个简单的活动流。

Given that User abc creates a post 123
And User xyz comments on 123.  
Then user abc's activity stream should look something like:
<ul>
  <li>User <a href="/users/abc">abc</a> created a post <a href="/users/abc/posts/123">123</a></li>
  <li>User <a href="/users/xyz">xyz</a> commented on post <a href="/users/abc/posts/123">123</a></li>
</ul>

我更喜欢填充活动模型(而不是控制器中的多个查询)。我在帖子和评论模型中添加了一个“after_create”挂钩来填充活动:

class Post < ActiveRecord
  after_create do
    ActivityCreate(:user_id => self.user_id, :description => "created a post")
  end
end

我想存储创建路由和链接所需的特殊字段,例如 post_id 等,但我可能有更多想要的活动稍后跟踪、喜欢、不喜欢、收藏夹等,我想要一种灵活的方式来创建活动描述。

所以我的问题是,将 link_to 放入活动描述中的有效方法是什么?是否有任何 c 风格的 printf 方式来列出稍后评估的助手?

有点像这样:

description = [“User ? commented on post ?”, user_path(User.find(self.user_id)), post_path(Post.find(self.id))]

然后在模板端评估它?

I'm trying to implement a simple activity stream.

Given that User abc creates a post 123
And User xyz comments on 123.  
Then user abc's activity stream should look something like:
<ul>
  <li>User <a href="/users/abc">abc</a> created a post <a href="/users/abc/posts/123">123</a></li>
  <li>User <a href="/users/xyz">xyz</a> commented on post <a href="/users/abc/posts/123">123</a></li>
</ul>

I prefer to populate an activity model (rather than multiple queries in a controller). I added an 'after_create' hook to both the posts and comments models to populate the activity:

class Post < ActiveRecord
  after_create do
    ActivityCreate(:user_id => self.user_id, :description => "created a post")
  end
end

I thought of storing special fields that I would need to create routes and links, such as post_id, etc, but I may have more activities I want to track later, likes, dislikes, favorites, etc, and I want a flexible way of creating activity descriptions.

So my question is, what is an efficient way of putting the link_to into the activity description? Is there any c-style printf way of listing helpers to be evaluated later?

Sort of like this:

description = ["User ? commented on post ?", user_path(User.find(self.user_id)), post_path(Post.find(self.id))]

And then evaluate this on the template side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2024-12-15 20:56:02

这是我对你的案件的第一次联想。也许它足以激励你:)

class Activity < ActiveRecord::Base
  #this might be a comment or a post or something else
  belongs_to :changed_item, :polymorphic => true
  #who did it?
  belongs_to :user
  #what did he do?
  validates_presence_of :changes

  def msg
    #do fancy text generation
    if changes.new_record
      "#{user.name} created a #{changed_item.class}"
    else
      "#{user.name} changed #{changes.length} attributes of the #{changed_item.class} #{changed_item.title}"
    end
  end

  def initialize(model, user)
    #fetch changes and remember user and changed model
    self.changed_item = model
    self.user = user
    self.changes << model.changed_attributes.merge({:new_record => model.new_record?})
  end
end

class Post < ActiveRecord::Base
  after_save do
    #remember who to be blamed
    Activity.create(self, current_user)
  end
end

class User < ActiveRecord::Base
  #the users activities
  has_many :activities
end

This is my first association about your case. Perhaps it inspires you enough :)

class Activity < ActiveRecord::Base
  #this might be a comment or a post or something else
  belongs_to :changed_item, :polymorphic => true
  #who did it?
  belongs_to :user
  #what did he do?
  validates_presence_of :changes

  def msg
    #do fancy text generation
    if changes.new_record
      "#{user.name} created a #{changed_item.class}"
    else
      "#{user.name} changed #{changes.length} attributes of the #{changed_item.class} #{changed_item.title}"
    end
  end

  def initialize(model, user)
    #fetch changes and remember user and changed model
    self.changed_item = model
    self.user = user
    self.changes << model.changed_attributes.merge({:new_record => model.new_record?})
  end
end

class Post < ActiveRecord::Base
  after_save do
    #remember who to be blamed
    Activity.create(self, current_user)
  end
end

class User < ActiveRecord::Base
  #the users activities
  has_many :activities
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文