Douglas F Shearer

Rails Plugin: Acts As Follower


Followers are the new friends. Apps such as Twitter and Github have effectively enabled virtual stalking within their apps. So how can you do this easily without wasting time with your own join tables? Acts_As_Follower to the rescue!

Install

./script/plugin install git://github.com/tcocca/acts_as_follower.git

Usage

A service like Twitter would have a user model, which would be both a follower and followable…


  class User < ActiveRecord::Base
    acts_as_follower
    acts_as_followable
  end
  
  # In the console...
  user1.follow(user2)
  
  # Get the users following user2
  user2.followers # => # Array containing user1
  
  # Get the users user1 is following
  user1.following # => Array containing user2
  
  # Check following
  user1.following?(user2) # => true
  user2.following?(user1) # => false
  
  # Stop following
  user1.stop_following(user2)
  

Acts_As_Follower allows heterogeneous follow relationships too, so a user could follow repositories and users, say, in an application like Github.

This plugin saved me a lot of time during the start-up of a recent project, and it hopefully should do the same for you. For more usage examples, see the README.

 

Comments


Gravatar

Shai Shefer

December 16 2008 17:09

Beautiful.

Can't begin to tell you 1.) how much I love this and 2.) how I wish I found this plugin 4 months ago.

:)

Gravatar

Shai Shefer

December 16 2008 17:19

My only question is this:

If I delete either a follower or a followable (in this example User 1) will the followings also be removed, or do I need to do an after_destroy to handle it?

Thanks again!

Gravatar

Douglas F Shearer

December 16 2008 20:16

Hi Shai Shefer.

Follow records should be destroyed or nullified when either the follower or followable is destroyed.

Unfortunately this had a bug in which was fixed by a patch I made yesterday. Until this is merged you can get it from my fork: http://github.com...

Gravatar

Shai Shefer

December 17 2008 14:09

Thanks! What I was looking for in the plugin files.

-Shai

Gravatar

Tom Cocca

December 28 2008 15:02

Hi Doug, just wanted to give you the update. The patch you submitted was merged into the master along with some other updates. Support for STI is continuing to improve and the code has been DRYed up a bit.

Thanks for your help on this plugin.

~ Tom

Gravatar

Louis-Pierre Dahito

February 17 2009 19:41

This is an excellent plugin... thx for sharing. It saves me a lot of time....

Gravatar

mike

April 20 2009 16:28

God this is fab. cheers for taking the time to post

Gravatar

Lance Vaughn

June 02 2009 16:27

Nice work! Thanks!

Add Your Comments


Commenting is closed for this entry.

 

You Are Here


Douglas F Shearer

This is the homepage of Douglas F Shearer, a software developer and mountainbike racer. More…

Hire Me!


I'm available for hire. Ruby, Java and PHP work, both remotely (Worldwide) and locally (Scotland). Find out more or email me.

Flickr Latest


Stay Informed


What is RSS?

Categories


  1. Bike (89)
  2. Coding (84)
  3. Other (46)

Top Tags