How to create a simple activity stream?

I am trying to implement a simple activity stream.

Given that User abc creates a post 123 And User xyz comments on 123. Then user abc 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 the action model (rather than multiple requests in the controller). I added the 'after_create' binding to the post and comment models to populate the activity:

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

I was thinking about storing special fields that I need to create routes and links, such as post_id, etc., but I may have more actions that I want to track later, like, dislike, favorites, etc. , and I want a flexible way to create operation descriptions.

So my question is, what is an efficient way to put link_to in the operation description? Is there any c-type style printing method that will be evaluated later?

Example:

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

And then evaluate it on the template side?

+4
source share
1 answer

This is my first association about your case. Perhaps this 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 
+2
source

All Articles