acts_as_follower is a plugin to allow any model to follow any other model. This is accomplished through a double polymorphic relationship on the Follow model. There is also built in support for blocking/un-blocking follow records.
Main uses would be for Users to follow other Users or for Users to follow Books, etc…
(Basically, to develop the type of follow system that GitHub has)
Run the following command:
script/plugin install git://github.com/tcocca/acts_as_follower.git
Run the generator:
This will generate a migration file as well as a model called Follow.
Make your model(s) that you want to allow to be followed acts_as_followable, just add the mixin:
class User < ActiveRecord::Base ... acts_as_followable ... end class Book < ActiveRecord::Base ... acts_as_followable ... end
Make your model(s) that can follow other models acts_as_follower
class User < ActiveRecord::Base ... acts_as_follower ... end
To have an object start following another use the following:
book = Book.find(1) user = User.find(1) user.follow(book) # Creates a record for the user as the follower and the book as the followable
To stop following an object use the following
user.stop_following(book) # Deletes that record in the Follow table
You can check to see if an object that acts_as_follower is following another object through the following:
user.following?(book) # Returns true or false
To get the total number (count) of follows for a user use the following on a model that acts_as_follower
user.follow_count # Returns and integer
To get follow records that have not been blocked use the following
user.all_follows # returns an array of Follow records
To get all of the records that an object is following that have not been blocked use the following
user.all_following # Returns an array of every followed object for the user, this can be a collection of different object types, eg: User, Book
To get all Follow records by a certain type use the following
user.follows_by_type('Book') # returns an array of Follow objects where the followable_type is 'Book'
To get all followed objects by a certain type use the following.
user.following_by_type('Book') # Returns an array of all followed objects for user where followable_type is 'Book', this can be a collection of different object types, eg: User, Book
There is also a method_missing to accomplish the exact same thing a following_by_type('Book') to make you life easier
user.following_users # exact same results as user.following_by_type('User')
To get all the followers of a model that acts_as_followable
book.followers # Returns an array of all the followers for that book, a collection of different object types (eg. type User or type Book)
To get just the number of follows use
To see is a model that acts_as_followable is followed by a model that acts_as_follower use the following
book.followed_by?(user) # Returns true if the current instance is followed by the passed record # Returns false if the current instance is blocked by the passed record or no follow is found
To block a follower call the following
book.block(user) # Blocks the user from appearing in the followers list, and blocks the book from appearing in the user.all_follows or user.all_following lists
To unblock is just as simple
To get all blocked records
book.blocks # Returns an array of blocked follower records (only unblocked) (eg. type User or type Book)
If you only need the number of blocks use the count method provided
Unblocking deletes all records of that follow, instead of just the :blocked attribute => false the follow is deleted. So, a user would need to try and follow the book again. I would like to hear thoughts on this, I may change this to make the follow as :blocked => false instead of deleting the record.
The Follow model has a set of named_scope's. In case you want to interface directly with the Follow model you can use them.
Follow.unblocked # returns all "unblocked" follow records Follow.blocked # returns all "blocked" follow records Follow.descending # returns all records in a descending order based on created_at datetime
This method pulls all records created after a certain date. The default is 2 weeks but it takes an optional parameter.
Follow.for_follower is a named_scope that is mainly there to reduce code in the modules but it could be used directly. It takes an object and will return all Follow records where the follower is the record passed in. Note that this will return all blocked and unblocked records.
If you don't need the blocked records just use the methods provided for this:
user.all_follows # or user.all_following
Follow.for_followable acts the same as its counterpart (for_follower). It is mainly there to reduce duplication, however it can be used directly. It takes an object that is the followed object and return all Follow records where that record is the followable. Again, this returns all blocked and unblocked records.
Again, if you don't need the blocked records use the method provided for this:
If you need blocked records only
If anyone has comments or questions please let me know (tom dot cocca at gmail dot com). If you have updates or patches or want to contribute I would love to see what you have or want to add.
Note on Patches/Pull Requests
Fork the project.
Make your feature addition or bug fix.
Add tests for it. This is important so I don't break it in a future version unintentionally (acts_as_follower uses Shoulda and Factory Girl)
Send me a pull request. Bonus points for topic branches.
Thanks to everyone for their interest and time in committing to making this plugin better.
dougal (Douglas F Shearer) - github.com/dougal
jdg (Jonathan George) - github.com/jdg
m3talsmith (Michael Christenson II) - github.com/m3talsmith
joergbattermann (Jörg Battermann) - github.com/joergbattermann
TomK32 (Thomas R. Koll) - github.com/TomK32
drcapulet (Alex Coomans) - github.com/drcapulet
jhchabran (Jean Hadrien Chabran) - github.com/jhchabran
Please let me know if I missed you.
Copyright © 2008 - 2009 ( tom dot cocca at gmail dot com ), released under the MIT license.