module Popular::Popular
Namespace for methods included in popular models
Public Instance Methods
Adds a friend to an instance’s friend’s list
@param [Object] new_friend a popular_model that the instance is not already friends with
@example
user = User.create name: "Justin" other_user = User.create name: "Jenny" user.befriend other_user user.friends_with? other_user #=> true
# File lib/popular/popular.rb, line 49 def befriend new_friend run_callbacks :befriend do friendships.create friend_id: new_friend.id, friend_type: new_friend.class.name end end
Adds a friend to an instance’s friend’s list Similar to .befriend, but will raise an error if the operation is not successful
@param [Object] new_friend a popular_model that the instance is not already friends with
@example
user = User.create name: "Justin" other_user = User.create name: "Jenny" user.befriend! other_user user.friends_with? other_user # => true
# File lib/popular/popular.rb, line 66 def befriend! new_friend run_callbacks :befriend do friendships.create! friend_id: new_friend.id, friend_type: new_friend.class.name end end
Helper method for finding whether or not the instance has befriended another given popular_model
Helper method for finding whether or not the instance has been befriended by another given popular_model
@param [Object] popular_model @return [Boolean] if the instance has been friended by another popular_model
@example
user = User.create name: "Justin" other_user = User.create name: "Jenny" user.friended_by? other_user #=> false other_user.befriend user user.friended_by? other_user #=> true
# File lib/popular/popular.rb, line 127 def friended_by? popular_model inverse_friends.include? popular_model end
Helper method for finding whether or not the instance has befriended another given popular_model
@param [Object] popular_model @return [Boolean] if the instance has popular_model as a friend
@example
user = User.create name: "Justin" other_user = User.create name: "Jenny" user.friends_with? other_user #=> false user.befriend other_user user.friends_with? other_user #=> true
# File lib/popular/popular.rb, line 146 def friends_with? popular_model friends.include? popular_model end
Helper method for determining whether instances are mutual friends
@param [Object] popular_model @return [Boolean] if both instances have befriended eachother
@example
user = User.create name: "Justin" other_user = User.create name: "Jenny" user.befriend other_user other_user.befriend user user.mutual_friends_with? other_user #=> true
# File lib/popular/popular.rb, line 105 def mutual_friends_with? popular_model friends_with?( popular_model ) && friended_by?( popular_model ) end
Removes a friend from an instance’s friend’s list
@param [Object] friend a popular_model in this instance’s friends list
@example
user = User.create name: "Justin" other_user = User.create name: "Jenny" user.befriend other_user user.unfriend other_user user.friends_with? other_user # => false
# File lib/popular/popular.rb, line 83 def unfriend friend run_callbacks :unfriend do friendships .where( friend_type: friend.class.name ) .where( friend_id: friend.id ) .first.destroy end end