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
-
#befriend(new_friend) ⇒ Object
Adds a friend to an instance’s friend’s list.
-
#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.
-
#friended_by?(popular_model) ⇒ Boolean
Helper method for finding whether or not the instance has befriended another given popular_model.
-
#friends_with?(popular_model) ⇒ Boolean
Helper method for finding whether or not the instance has befriended another given popular_model.
-
#mutual_friends_with?(popular_model) ⇒ Boolean
Helper method for determining whether instances are mutual friends.
-
#unfriend(friend) ⇒ Object
Removes a friend from an instance’s friend’s list.
Instance Method Details
#befriend(new_friend) ⇒ Object
Adds a friend to an instance’s friend’s list
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
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
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
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
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
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 |