Timeline Fu

Realized nearly all of my projects involved logging user actions and having news feeds similar to Facebook. Two years ago, it didn’t seem sensible to use a gem for just that because all you need is to create a module and helpers to log actions (on your model of course). A common mistake is adding code for logging actions, sending emails on controllers. Sometimes when you are young, you do stupid things.

Timeline Fu is a gem that allows you to easily build timelines, much like GitHub’s news feed or event Facebook news feed.

  class Post < ActiveRecord::Base
    #...
    belongs_to :author, :class_name => 'Person'
    fires :new_post, :on    => :create, :actor => :author
  end

When a new post is created, the action is saved with the author as the actor and the post as the subject. There are also secondary subjects which makes sense if your model is a Comment model and you have a post. The secondary subject can be the post.

Destroying associated records

Of course, it is easy in Rails! Unless you forgot to add it up.

  class Post < ActiveRecord::Base
    #...
    belongs_to :author, :class_name => 'Person'
     has_many :timeline_events, :as => :subject, :dependent => :destroy
    fires :new_post, :on    => :create, :actor => :author
  end

 on  post_spec.rb
   #see shoulda and Rspec
   describe "assert associations" do
     it { should have_many(:timeline_events) }
   end