Module: HasFriends::InstanceMethods
- Defined in:
- lib/has_friends.rb
Instance Method Summary collapse
- #be_friends_with(friend) ⇒ Object
-
#destroy_friendship_with(friend) ⇒ Object
Destroyes (in both ways) the friendship.
- #friends?(friend) ⇒ Boolean
- #friends_in_common_with(user) ⇒ Object
- #friends_not_in_common_with(user) ⇒ Object
- #friends_of_friends ⇒ Object
- #friendship_for(friend) ⇒ Object
- #is?(friend) ⇒ Boolean
Instance Method Details
#be_friends_with(friend) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/has_friends.rb', line 20 def be_friends_with(friend) # no user object return nil, Friendship::STATUS_FRIEND_IS_REQUIRED unless friend # should not create friendship if user is trying to add himself return nil, Friendship::STATUS_IS_YOU if is?(friend) # should not create friendship if users are already friends return nil, Friendship::STATUS_ALREADY_FRIENDS if friends?(friend) # retrieve the friendship request friendship = self.friendship_for(friend) # let's check if user has already a friendship request or have removed request = friend.friendship_for(self) # friendship has already been requested return nil, Friendship::STATUS_ALREADY_REQUESTED if friendship && friendship.requested? # friendship is pending so accept it if friendship && friendship.pending? friendship.accept! request.accept! return friendship, Friendship::STATUS_FRIENDSHIP_ACCEPTED end # we didn't find a friendship, so let's create one! friendship = self.friendships.create(:friend_id => friend.id, :status => 'requested') # we didn't find a friendship request, so let's create it! request = friend.friendships.create(:friend_id => id, :status => 'pending') return friendship, Friendship::STATUS_REQUESTED end |
#destroy_friendship_with(friend) ⇒ Object
Destroyes (in both ways) the friendship
84 85 86 87 88 89 |
# File 'lib/has_friends.rb', line 84 def destroy_friendship_with(friend) friendship = friendship_for(friend) other_friendship = friend.friendship_for(self) friendship.destroy other_friendship.destroy end |
#friends?(friend) ⇒ Boolean
56 57 58 59 |
# File 'lib/has_friends.rb', line 56 def friends?(friend) friendship = friendship_for(friend) friendship && friendship.accepted? end |
#friends_in_common_with(user) ⇒ Object
69 70 71 |
# File 'lib/has_friends.rb', line 69 def friends_in_common_with(user) self.friends.select { |friend| user.friends.include?(friend) } end |
#friends_not_in_common_with(user) ⇒ Object
73 74 75 |
# File 'lib/has_friends.rb', line 73 def friends_not_in_common_with(user) self.friends.reject { |friend| user.friends.include?(friend) or user == friend } end |
#friends_of_friends ⇒ Object
77 78 79 80 81 |
# File 'lib/has_friends.rb', line 77 def friends_of_friends self.friends.collect do |friend| friend.friends_not_in_common_with(self) end.flatten.uniq end |
#friendship_for(friend) ⇒ Object
61 62 63 |
# File 'lib/has_friends.rb', line 61 def friendship_for(friend) friendships.first :conditions => {:friend_id => friend.id} end |
#is?(friend) ⇒ Boolean
65 66 67 |
# File 'lib/has_friends.rb', line 65 def is?(friend) self.id == friend.id end |