Models relationships between AR models. Allows you to follow, friend, and block other AR's. Consists of two mixins: acts_as_followable and acts_as_friend. These options allow an AR to inherit either a twitter-like follower system or a facebook-like friend system.
Install the gem
gem install party_boy
Run the generator
This will generate a migration file as well as the necessary Relationship class in your models folder.
Add the appropriate mixin to your models:
class Waterboy < ActiveRecord::Base ... acts_as_followable ... end class Quarterback < ActiveRecord::Base ... acts_as_friend ... end
To allow a model (A) to follow another (B), add acts_as_follow to at least model A. Now, you can follow any other model in your project:
a = Waterboy.find 1 b = Quarterback.find 2 a.follow(b)
To stop following, simply just:
Or to block the relationship:
To find out if there is a relationship between two models, use the methods:
To retrieve a set of models based on the relationships, use:
STI is also handled by party_boy. The relationship is always stored using the super-most class. However, relationships to inheriting classes can also be retrieved. Do so by passing in the type(s):
class Quarterback < User; end class Cheerleader < User; end class Waterboy < User; end
In string form
a.followers('users') a.following(%w(users quarterbacks))
Or in class form
a.followers(User) b.following([User, Quarterback, Cheerleader])
On top of accessing relationships through followers / following methods, party_boy will dynamically filter the results based on the combined class name:
a.quarterback_followers # returns all followers of type quarterback b.cheerleader_followers # returns all followers of type cheerleader a.following_quarterbacks # returns all the quarterbacks 'a' is following b.following_cheerleaders # returns all the cheerleaders 'b' is following
Docs coming soon. I don't need it, do you?
Copyright © 2010 Mike Nelson. See LICENSE for details.