Module: Friendable::UserMethods
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/friendable/user_methods.rb
Instance Method Summary collapse
-
#friend!(resource, options = {}) ⇒ Object
Adds the given user to your friend list.
-
#friend?(resource) ⇒ Boolean
Returns if the given user is friend of you or not.
-
#friend_ids ⇒ Object
Returns an array object of the friend ids of your friends.
-
#friends ⇒ Object
Returns an collection of user ressources(ActiveRecord::Relation).
-
#friends_count ⇒ Object
Returns the number of the user’s friends.
-
#friendship_with(target_resource) ⇒ Object
Returns a friendship object that indicates the relation from a user to another user.
-
#friendships ⇒ Object
Returns an array of friendship objects.
-
#unfriend!(resource) ⇒ Object
Removes the given user from your friend list.
Instance Method Details
#friend!(resource, options = {}) ⇒ Object
Adds the given user to your friend list. If the given user is already a friend of you and different options are given, the existing options will be replaced with the new options. It returns an object of Friendable::Friendship.
Examples
current_user.friends.include?(target_user) # => false
current_user.friend!(target_user) # => #<Friendable::Friendship>
current_user.friends.include?(target_user) # => true
friendship = current_user.friend!(target_user, :foo => "bar")
friendship.foo # => "bar"
friendship. # => NoMethodError
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/friendable/user_methods.rb', line 109 def friend!(resource, = {}) # TODO: in the near future, I want to change to something like this: # redis.multi do # Friendship.new(self, resource, options).save(inverse: true) # end Friendship.new(self, resource, ).tap do |friendship| redis.multi do friendship.save Friendship.new(resource, self).save end end end |
#friend?(resource) ⇒ Boolean
Returns if the given user is friend of you or not.
Examlpes
current_user.friend?(target_user) # => false
current_user.friend!(target_user)
current_user.friend?(target_user) # => true
91 92 93 |
# File 'lib/friendable/user_methods.rb', line 91 def friend?(resource) @_raw_friend_ids.try(:include?, resource.id.to_s) || @_raw_friend_hashes.try(:has_key?, resource.id.to_s) || redis.hexists(friend_list_key, resource.id) end |
#friend_ids ⇒ Object
Returns an array object of the friend ids of your friends.
Examples
current_user.friend!(target_user)
current_user.friend_ids.include?(target_user.id) # => true
21 22 23 |
# File 'lib/friendable/user_methods.rb', line 21 def friend_ids @_friend_ids ||= (@_raw_friend_hashes ? @_raw_friend_hashes.keys : raw_friend_ids).map(&:to_i) end |
#friends ⇒ Object
Returns an collection of user ressources(ActiveRecord::Relation).
Examples
current_user.friends # => [#<User id: 1, ...>, #<User id: 2, ...>, ...]
current_user.friends.first # => #<User id: 1, ...>
current_user.friends.class # => ActiveRecord::Relation
32 33 34 |
# File 'lib/friendable/user_methods.rb', line 32 def friends @_friends ||= Friendable.resource_class.where(:id => friend_ids) end |
#friends_count ⇒ Object
Returns the number of the user’s friends.
Examples
current_user.friends_count # => 0
current_user.friend!(target_user)
current_user.friends_count # => 1
78 79 80 |
# File 'lib/friendable/user_methods.rb', line 78 def friends_count @_raw_friend_ids.try(:count) || @_raw_friend_hashes.try(:count) || redis.hlen(friend_list_key) end |
#friendship_with(target_resource) ⇒ Object
Returns a friendship object that indicates the relation from a user to another user.
Examples
current_user.friend!(target_user, :foo => "bar")
friendship = current_user.friendship_with(target_user)
friendship.friend == target_user # => true
friendship.foo # => "bar"
friendship. # => NoMethodError
60 61 62 63 64 65 66 67 68 |
# File 'lib/friendable/user_methods.rb', line 60 def friendship_with(target_resource) raw_friendship = @_raw_friend_hashes.try(:[], target_resource.id.to_s) || redis.hget(friend_list_key, target_resource.id) if raw_friendship Friendship.deserialize!(self, target_resource, raw_friendship) else raise Friendable::FriendshipNotFound, "user:#{self.id} is not a friend of the user:#{target_resource.id}" end end |
#friendships ⇒ Object
Returns an array of friendship objects. Currently this method does not support pagination.
Examples
current_user.friendships # => [#<Friendable::Friendship>, ...]
current_user.friendships.first # => #<Friendable::Friendship>
current_user.friendships.class # => Array
44 45 46 47 48 |
# File 'lib/friendable/user_methods.rb', line 44 def friendships @_friendships ||= Friendable.resource_class.find(raw_friend_hashes.keys).map do |resource| Friendship.deserialize!(self, resource, raw_friend_hashes[resource.id.to_s]) end end |
#unfriend!(resource) ⇒ Object
Removes the given user from your friend list. If the given user is not a friend of you, it doesn’t affect anything.
Examples
current_user.friend!(target_user)
current_user.friends.include?(target_user) # => true
current_user.unfriend!(target_user)
current_user.friends.include?(target_user) # => false
132 133 134 135 136 137 |
# File 'lib/friendable/user_methods.rb', line 132 def unfriend!(resource) redis.multi do redis.hdel(friend_list_key, resource.id) redis.hdel(resource.friend_list_key, self.id) end end |