Module: HasManyFriends::UserExtensions::InstanceMethods

Defined in:
lib/has_many_friends.rb

Instance Method Summary collapse

Instance Method Details

#accept_friendship_with(friend) ⇒ Object

Accepts a user object and updates an existing friendship to be accepted.



108
109
110
# File 'lib/has_many_friends.rb', line 108

def accept_friendship_with(friend)
  self.friendship(friend).update_attribute(:accepted_at, Time.now)
end

#become_friends_with(friend) ⇒ Object

Accepts a user object and creates a friendship between both users. This method bypasses the request stage and makes both users friends without needing to be accepted.



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/has_many_friends.rb', line 121

def become_friends_with(friend)
  unless self.is_friends_with?(friend)
    unless self.is_pending_friends_with?(friend)
      Friendship.create!(:friendshipped_by_me => self, :friendshipped_for_me => friend, :accepted_at => Time.now)
    else
      self.friendship(friend).update_attribute(:accepted_at, Time.now)
    end
  else
    self.friendship(friend)
  end
end

#delete_friendship_with(friend) ⇒ Object

Accepts a user object and deletes a friendship between both users.



114
115
116
# File 'lib/has_many_friends.rb', line 114

def delete_friendship_with(friend)
  self.friendship(friend).destroy if self.is_friends_or_pending_with?(friend)
end

#friendsObject

Returns a list of all of a users accepted friends.



54
55
56
# File 'lib/has_many_friends.rb', line 54

def friends
  self.friends_for_me + self.friends_by_me
end

#friendship(friend) ⇒ Object

Accepts a user object and returns the friendship object associated with both users.



77
78
79
# File 'lib/has_many_friends.rb', line 77

def friendship(friend)
  Friendship.find(:first, :conditions => ['(user_id = ? AND friend_id = ?) OR (friend_id = ? AND user_id = ?)', self.id, friend.id, self.id, friend.id])
end

#is_friends_or_pending_with?(friend) ⇒ Boolean

Accepts a user object and returns true if both users are friends regardless of acceptance.

Returns:

  • (Boolean)


95
96
97
# File 'lib/has_many_friends.rb', line 95

def is_friends_or_pending_with?(friend)
  self.pending_or_accepted_friends.include?(friend)
end

#is_friends_with?(friend) ⇒ Boolean

Accepts a user object and returns true if both users are friends and the friendship has been accepted.

Returns:

  • (Boolean)


83
84
85
# File 'lib/has_many_friends.rb', line 83

def is_friends_with?(friend)
  self.friends.include?(friend)
end

#is_pending_friends_with?(friend) ⇒ Boolean

Accepts a user object and returns true if both users are friends but the friendship hasn’t been accepted yet.

Returns:

  • (Boolean)


89
90
91
# File 'lib/has_many_friends.rb', line 89

def is_pending_friends_with?(friend)
  self.pending_friends.include?(friend)
end

#online_friendsObject

Return a list of all friends who are currently online. This won’t return anything unless you passed the :online_method option to has_many_friends.



61
62
63
# File 'lib/has_many_friends.rb', line 61

def online_friends
  self.friends_by_me.online + self.friends_for_me.online
end

#pending_friendsObject

Returns a list of all pending friendships.



66
67
68
# File 'lib/has_many_friends.rb', line 66

def pending_friends
  self.pending_friends_by_me + self.pending_friends_for_me
end

#pending_or_accepted_friendsObject

Returns a full list of all pending and accepted friends.



71
72
73
# File 'lib/has_many_friends.rb', line 71

def pending_or_accepted_friends
  self.friends + self.pending_friends
end

#request_friendship_with(friend) ⇒ Object

Accepts a user object and creates a friendship request between both users.



101
102
103
104
# File 'lib/has_many_friends.rb', line 101

def request_friendship_with(friend)
  Friendship.create!(:friendshipped_by_me => self, 
                     :friendshipped_for_me => friend) unless self.is_friends_or_pending_with?(friend) || self == friend
end