Activity Feeds with Rails

Originally published at: http://www.sitepoint.com/activity-feeds-rails/
Screenshot 2015-02-22 08.05.08

Activity feeds are all over the place. For example, Github’s main page lists all of the activity in your social coding network: one of your friends created or cloned a repo, a new issue was opened, your repo was forked or starred, etc.

Twitter displays the latest tweets, retweets, and replies.

That’s a convenient and easy way for users to learn about recent updates. How about trying to replicate the same functionality with Rails? Turns out, there is a convenient gem that makes the development process very easy!

In this article, I am going to talk about public_activity, a gem created by Piotrek Okoński to easily track activity for models (and more, actually). I will show you how to easily create an activity feed and employ various features of public_activity to extend the app further.

Before proceeding, I wanted to make a small note. Some months ago, I wrote an article Versioning with PaperTrail that covered paper_trail, a gem used to implement version control of an application’s models. In some ways, paper_trail is similar to public_activity and could be used to implement a solution similar to the one presented in this article. However, paper_trail is aimed at crafting versioning systems, whereas public_activity was created specifically to implement activity feeds.

The working demo is available at sitepoint-public-activity.herokuapp.com.

The source code is available on GitHub.

Continue reading this article on SitePoint

2 Likes

Good stuff! As someone who has built and worked with activity streams for years, I will say this is a good start. One thing I would suggest looking at is the Activity Streams protocol specification - http://activitystrea.ms. This provides a standard data format for activity stream data that can then be used with any standardized activity streams tool.

Once you start to move beyond simple streams - e.g. to add different types of activity, enable commenting, and especially allow following and building of feeds, it’s my belief that you shouldn’t try to build it yourself. It starts to get very complex to enable that level of functionality and maintain any kind of performance. I actually built a company around solving this problem - it’s called Collabinate (http://www.collabinate.com). We provide an API that allows you to build streams for any object in an application, manages the relationship of followers to streams, lets you pull back feeds in real time, enables likes and comments, utilizes the Activity Streams protocol, and does it at huge scale if necessary (billions of objects). We should talk some time if you’re interested in learning more about it.

Thanks again for the article!

Thank you!

Your service seems like a nice solution - I may be interested in writing an article about it. Could you contact me via e-mail (http://radiant-wind.com - e-mail and some other contact options are listed here) if that suits you?

Good stuff…
Anyway, I checked your github apps for this tutorial, seems there is folder public_activity for the view.
A bit confused about that.

Thanks

Sorry, didn’t quite understand. I have no such folder: https://github.com/bodrovis/SitePoint-PublicActivity/tree/master/app/views because I am sticking with I18n for this demo.

Yeah, sorry my bad, I did not read it carefully. So I found out you deleted that because of using the I18n… Cool stuff. Thank you very much.

Btw, is is possible to make it to more than 1 model ? like 2-3 models?
Thanks

By the way, I have couple of questions
First one, if you have 2 users type (user and admin) how do you set the owner?
Second, how do you filter them on the render activity or the view?
and lastly, this is applicable to many models?

Thanks

Nope, with more than 1 model you will have to use partial views like described here: https://github.com/pokonski/public_activity#activity-views

You will want to tweak this

 @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]

and this code

tracked owner: Proc.new { |controller, model| controller.current_user ? controller.current_user : nil }[...]

accordingly. Just more checkings come into play - you also check if the person is logged in as admin.

By the owner field, if I understood the question correctly. It has the polymorphic association (https://github.com/pokonski/public_activity/blob/4ea9f56628a7699370a6a3daad31ddf4b076a321/lib/public_activity/orm/active_record/activity.rb#L13) and you can use it for find only activities for admin or user for example.

Of course, multiple models may be used. Just remember to use partials, not I18n fallbacks.

Okaaay
I have tried to play around this. Seems pretty good.

Thank you very much for your time

cheers

I am big fans of you :smiley:

Oh, thank you for kind words! Don’t hesitate to contact me if anything else will seem unclear.

1 Like

AWESOME article! Really gave me a great grasp on the activity_feed. Is there a way to 1) limit liking to one time per post so that if you click on “Like it” a second time it unlikes it, like Facebook 2) show names of likers next to like button so you know who is liking it without having to scroll down the activity feed? Might be a nice expansion to your wonderful tutorial because I can’t find these answers anywhere!

Thank you! :slight_smile:

1&2. Of course that is possible, but this is not related to activity feeds so I did not consider add such info to the currect article. However I think that is a nice suggestion for a new topic so I’ll probably work on this in some time, maybe expanding a bit further. It is really great to receive so many feedback!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.