Module: Popular::Popular

Extended by:
ActiveSupport::Concern
Defined in:
lib/popular/popular.rb

Overview

Namespace for methods included in popular models

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#befriend(new_friend) ⇒ Object

Adds a friend to an instance’s friend’s list

Examples:

user = User.create name: "Justin"
other_user = User.create name: "Jenny"
user.befriend other_user

user.friends_with? other_user #=> true

Parameters:

  • new_friend (Object)

    a popular_model that the instance is not already friends with



49
50
51
52
53
# 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

#befriend!(new_friend) ⇒ Object

Adds a friend to an instance’s friend’s list Similar to .befriend, but will raise an error if the operation is not successful

Examples:

user = User.create name: "Justin"
other_user = User.create name: "Jenny"
user.befriend! other_user

user.friends_with? other_user # => true

Parameters:

  • new_friend (Object)

    a popular_model that the instance is not already friends with



66
67
68
69
70
# 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

#friended_by?(popular_model) ⇒ Boolean

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

Examples:

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

Parameters:

  • popular_model (Object)

Returns:

  • (Boolean)

    if the instance has been friended by another popular_model



127
128
129
# File 'lib/popular/popular.rb', line 127

def friended_by? popular_model
  inverse_friends.include? popular_model
end

#friends_with?(popular_model) ⇒ Boolean

Helper method for finding whether or not the instance has befriended another given popular_model

Examples:

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

Parameters:

  • popular_model (Object)

Returns:

  • (Boolean)

    if the instance has popular_model as a friend



146
147
148
# File 'lib/popular/popular.rb', line 146

def friends_with? popular_model
  friends.include? popular_model
end

#mutual_friends_with?(popular_model) ⇒ Boolean

Helper method for determining whether instances are mutual friends

Examples:

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

Parameters:

  • popular_model (Object)

Returns:

  • (Boolean)

    if both instances have befriended eachother



105
106
107
# File 'lib/popular/popular.rb', line 105

def mutual_friends_with? popular_model
  friends_with?( popular_model ) && friended_by?( popular_model )
end

#unfriend(friend) ⇒ Object

Removes a friend from an instance’s friend’s list

Examples:

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

Parameters:

  • friend (Object)

    a popular_model in this instance’s friends list



83
84
85
86
87
88
89
90
# 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