module MongoMapper::Plugins::Friendable::InstanceMethods

Public Instance Methods

add_friend!(friend) click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 21
def add_friend!(friend)
  return false if friend == self

  User.push_uniq(self.id, 'friend_list.following_ids' => friend.id)
  User.push_uniq(friend.id, 'friend_list.followers_ids' => self.id)
  User.increment(self.id, 'following_count' => 1)
  User.increment(friend.id, 'followers_count' => 1)

  on_add_friend(friend)
end
followers() click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 51
def followers
  User.find(self.friend_list.followers_ids)
end
following() click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 55
def following
  User.find(self.friend_list.following_ids)
end
following?(user) click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 59
def following?(user)
  self.friend_list.following_ids.include?(user.id)
end
friendable?() click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 19
def friendable?; true; end
on_add_friend(friend) click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 32
def on_add_friend(friend)
  raise NotImplementedError
end
on_remove_friend(friend) click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 47
def on_remove_friend(friend)
  raise NotImplementedError
end
remove_friend!(friend) click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 36
def remove_friend!(friend)
  return false if friend == self
  
  User.pull(self.id, 'friend_list.following_ids' => friend.id)
  User.pull(friend.id, 'friend_list.followers_ids' => self.id)
  User.decrement(self.id, 'following_count' => 1)
  User.decrement(friend.id, 'followers_count' => 1)

  on_remove_friend(friend)
end

Protected Instance Methods

create_friend_list() click to toggle source
# File lib/mongo_mapper/plugins/friendable.rb, line 64
def create_friend_list
  self.friend_list = FriendList.new unless self.friend_list.present?
end