Amistad – Facebook like friendship management in Ruby on Rails

Today I’ve decided to write something about the Ruby Gems that I used. So I’ve started this series (Ruby Gems). This is the first post of the series. I will try to write regularly.

When I was working on my pet project MovieMates, I needed to implement a friend relationship. Initially, I was going to do it myself. However, later I thought I should, at least, google a bit to check if there is already some similar solutions. Then I found Amistad.

The current version of Amistad supports ActiveRecord 3.0.x, Mongoid 3.0.x and MongoMapper 0.12.0.

Installations

Add following line to your Gemfile

gem 'amistad'

and then run

bundle install

Setting up 

[tab:ActiveRecord]

If you are using ActiveRecord, you will need a table for managing this. This gem has a generator that will write a migration script for you. Simply run the following command in your project directory

rails g amistad:install

Then, migrate your database

rake db:migrate

To activate this in your model:

class User < ActiveRecord::Base
include Amistad::FriendModel
end

[tab:Mongoid]
Configuring Mongoid model is simpler.

class User
  include Mongoid::Document
  include Amistad::FriendModel
end

[tab:MongoMapper]
Configuring Mongoid model is simpler.

class User
  include MongoMapper::Document
  include Amistad::FriendModel
end

[tab:END]

Usages

User A invites User B to be a friend

@userA.invite @userB

Invited friendship will remain pending. Other user needs to accept/reject the request.

User C blocks User A

@userC.block @userA

List all blocks by User C

@userC.blocked

Check if User A is blocked by User C

@userC.blocked? @userA

Unblock User A

@userC.unblock @userA

User B accepts invitation of User A

@userB.approve @userA

Find all friends of B

@userB.friends

Checking friendship of A & B (if A & B are friends)

@userB.friend_with? @userA

Mutual Friends (Find common friends between A and C)

@userA.common_friends_with @userC

Now A, B are friends. Now User B deletes User A as friend

@userB.remove_friendship @userA

There are other methods to check pending friends, connected friends, invitations etc. Please check the wiki pages of the gem to all the API information.