Pre-productioon plugin released to the public.
The basic idea is that there are two classes of users.
Fans, and mutual fans. If two people are fans, they are mutual fans, and thereby in my definition, friends.
Make sure you create a friendship model and this migration:
class CreateFriendships < ActiveRecord::Migration
def self.up
create_table :friendships do |t|
t.integer "user_id", :null => false
t.integer "friend_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.timestamps
end
add_index :friendships, :user_id
add_index :friendships, :friend_id
end
def self.down
drop_table :friendships
end
end
#friendship.rb
class Friendship < ActiveRecord::Base
belongs_to :friendshipped_by_me, :foreign_key => :user_id, :class_name => "User"
belongs_to :friendshipped_for_me, :foreign_key => :friend_id, :class_name => "User"
end
Also add this in your user model:
#user.rb
acts_as_fischyfriend
require File.dirname(__FILE__) + '/../spec_helper'
describe Friendship do
before(:each) do
@friendship = Friendship.new
end
it "should be valid" do
@friendship.should be_valid
end
end
describe Friendship, "between two users" do
fixtures :users
before(:each) do
@quentin = users(:quentin)
@bob = users(:aaron)
@wtf = User.create!(:login => 'a_user',
:password => 'password',
:password_confirmation => 'password',
:email => 'poop@poop.com')
end
it "should acknowledge there is a friendship between them" do
@bob.add_friend @quentin
@bob.add_friend @wtf
@quentin.add_friend @bob
@bob.reload
@bob.is_a_fan_of.should include(@wtf)
@bob.is_a_fan_of.should_not include(@quentin)
@bob.pending_mutual_friends.should include(@wtf)
@bob.pending_mutual_friends.should_not include(@quentin)
@wtf.mutual_friends.should be_empty
@wtf.fans_of_me.should include(@bob)
@quentin.reload
@quentin.mutual_friends.should include(@bob)
@quentin.mutual_friends.should_not include(@wtf)
@quentin.destroy_friendship_with @bob
@quentin.reload
@quentin.friends_by_me.should_not include(@bob)
end
it "should be able to tell you if two people are mutual friends" do
@bob.add_friend @quentin
@bob.add_friend @wtf
@quentin.add_friend @bob
@bob.is_mutual_friends_with?(@quentin).should eql(true)
@bob.is_mutual_friends_with?(@wtf).should eql(false)
end
end
August 3, 2008
- Accepted changeset 868bc by pogopuffin
- Added in specs against above changeset
- Tested against Rails 2.1.0 w/ passes
- Updated migration example with indexes, is this wise? I’m not a database wiz here.
March 14, 2008
- First Release
Copyright © 2008 [Daniel Fischer] / http://www.danielfischer.com, released under the MIT license