party_boy

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.

Installation

If you're running rails 2 use gem version 0.3.2 or the rails-2 branch.

Install the gem gem install party_boy

Run the generator rails generate party_boy

This will generate a migration file as well as the necessary Relationship class in your models folder.

Usage

Setup

Add the appropriate mixin to your models: class Waterboy < ActiveRecord::Base ... acts_as_followable ... end

class Quarterback < ActiveRecord::Base ... acts_as_friend ... end


acts_as_follower

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: a.unfollow(b)

Or to block the relationship: b.block(a)

To find out if there is a relationship between two models, use the methods: a.following?(b) b.followed_by?(a)

To retrieve a set of models based on the relationships, use: a.following b.followers

STI

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:

class Quarterback < User; end class Cheerleader < User; end class Waterboy < User; end

In string form a.followers('users') => [#<User id: 1, created_at: "2010-01-27 01:09:42", updated_at: "2010-01-27 01:09:42">] a.following('quarterbacks') => [#<Quarterback id: 3, created_at: "2010-01-27 01:09:42", updated_at: "2010-01-27 01:09:42">] Or in class form a.followers(User) => [#<User id: 1, created_at: "2010-01-27 01:09:42", updated_at: "2010-01-27 01:09:42">] b.following(Quarterback) => [#<Quarterback id: 3, created_at: "2010-01-27 01:09:42", updated_at: "2010-01-27 01:09:42">]

STI - part deux

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


acts_as_friend

To allow two models to become friends (requiring acceptance), add acts_as_friend to both. Now you can create a relationship between the two models by:

class Bob < ActiveRecord::Base acts_as_friend end

class Joe < ActiveRecord::Base acts_as_friend end

b = Bob.create j = Joe.create

b.request_friendship(j)

b.pending?(j) => true

b.outgoing_friend_requests # or j.incoming_friend_requests => [#<Relationship id: 1, :requestor_id: 1, :requestor_type: 'Bob', requestee_id: 1, requestee_type: 'Joe', restricted: true>]

j.accept_friendship(b)

b.friends => [#<Joe id: 1>]

Copyright (c) 2010 Mike Nelson. See LICENSE for details.