Simple Activity Stream Implementation in Rails

There are many ways to tackle the Facebook style activity stream feature for your app. The simplest approach, which you can tack on at almost any moment, is what I’ll describe here. You don’t have to create a new model for news feed items or create any polymorphic associations. You simply query for the records on separate models you would like aggregated in the stream, and render a partial for that record, which Rails will map for you.

# in app/controllers/home_controller.rb
def index
 @activity_stream = (Forum.find(:all, :limit => 10, :conditions=>["created_at > ?", 1.day.ago]) + ForumPost.find(:all, :limit => 10)).sort_by {|item| - item.created_at.to_i}
end

The sort_by method at the end places all of your feed item into descending chronological order. Omit the minus sign and they will be sorted in ascending order.

sort_by {|item| - item.created_at.to_i} # in descending chronological order

Show your latest activity in your views…

#app/views/home/index.html.erb
<%= render @activity_stream rescue nil %>

This assumes that you will have created the partials for each resource using Rails conventions i.e., you have in created the partials for the models like so app/views/_[the model name].html.erb. In this instance, you should have app/views/forums/_forum.html.erb and app/views/forum_posts/_forum_post.html.erb

You can rename the partials to something like _stream_forum.html.erb, if you already have the forum partial gainfully employed. You’ll need to change the view and prob. throw it into a loop to either check for a condition or use _stream_[model name].html.erb as the convention for any stream items.

19 Nov 2009, 1:59pm
Business:
by bseanvt

leave a comment

Adding RSS Graffiti to my Facebook Page

I added the RSS Graffiti application to my Facebook account. It’s a nifty app that lets you add any valid Rss/Atom feed to your profile (includes publishing to your wall) and to your pages.

Facebook Dark Launch

"Dark Launch"

"Dark Launch"

Facebook uses an interesting method for testing when rolling out new features. This method, which I first read about on the Facebook engineering blog, is refered to as a “dark launch”. It’s an interesting title and more interesting is that Facebook users are testing new feature rollouts without realizing it. Because Facebook has so many users it is difficult to stress test accurately. However, to simulate high load on new features, Facebook will pop some code on the users homepage and have it function off screen or render nothing at all. This simulates a lot of people using the product (because they are) and they can accurately, or more accurately, determine bottlenecks and work out kinks.

Both the launch of the Facebook Chat application and their integration with CNN for the 09 Innauguration of Barack Obama were “dark launch” products.

I’ve never heard the term before but it could be a well known strategy. Nonetheless, cool stuff Facebook.

The feed I got the info from was in my private profile so I don’t have a link. Join the “Engineering at Facebook” group to read the article. It might be publically available but I’m not sure.