Module: Circle::InstanceMethods

Defined in:
lib/circle/circle.rb

Instance Method Summary collapse

Instance Method Details

#accept_friend_request(friend) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/circle/circle.rb', line 82

def accept_friend_request(friend)
  return nil, Circle::Friendship::STATUS_CANNOT_ACCEPT unless can_accept_friend_request? rescue nil
  friendship = self.friendship_with(friend)
  if friendship.try(:pending?)
    requested = friend.friendship_with(self)

    ActiveRecord::Base.transaction do
      friendship.accept!
      requested.accept! unless requested.accepted?
    end

    return friendship, Circle::Friendship::STATUS_FRIENDSHIP_ACCEPTED
  else
    return nil, Circle::Friendship::STATUS_NOT_FOUND
  end
end

#befriend(friend) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/circle/circle.rb', line 25

def befriend(friend)
  return nil, Circle::Friendship::STATUS_FRIEND_IS_REQUIRED unless friend
  return nil, Circle::Friendship::STATUS_FRIEND_IS_YOURSELF if self.id == friend.id
  return nil, Circle::Friendship::STATUS_ALREADY_FRIENDS if friends?(friend)
  return nil, Circle::Friendship::STATUS_BLOCKED if friend.blocked?(self) || blocked?(friend)
  return nil, Circle::Friendship::STATUS_CANNOT_SEND unless can_send_friend_request? rescue nil

  friendship = self.friendship_with(friend)
  request = friend.friendship_with(self)

  return nil, Circle::Friendship::STATUS_ALREADY_REQUESTED if friendship && friendship.requested?

  if friendship && friendship.pending?
    return nil, Circle::Friendship::STATUS_CANNOT_ACCEPT unless can_accept_friend_request? && friend.can_accept_friend_request? rescue nil

    ActiveRecord::Base.transaction do
      friendship.accept!
      request.accept!
    end

    return friendship, Circle::Friendship::STATUS_FRIENDSHIP_ACCEPTED
  end

  if friendship && (friendship.denied? || friendship.blocked?)
    friendship.update_attributes({status: 'requested', requested_at: Time.now})
    request.update_attributes({status: 'pending', requested_at: Time.now})
  else
    ActiveRecord::Base.transaction do
      friendship = self.friendships.create(friend_id: friend.id, status: 'requested', requested_at: Time.now)
      request = friend.friendships.create(friend_id: id, status: 'pending', requested_at: Time.now)
    end
  end

  return friendship, Circle::Friendship::STATUS_REQUESTED
end

#block(friend) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/circle/circle.rb', line 114

def block(friend)
  request = friendship_with(friend)
  if request
    ActiveRecord::Base.transaction do
        request.block!(true)
        friend.friendship_with(self).try(:block!)
    end
    request.reload
    return request, Circle::Friendship::STATUS_BLOCKED
  else
    return nil, Circle::Friendship::STATUS_NOT_FOUND
  end
end

#blocked?(friend) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/circle/circle.rb', line 70

def blocked?(friend)
  !!users_blocked.where(blocked_user_id: friend).first
end

#deny_friend_request(friend) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/circle/circle.rb', line 99

def deny_friend_request(friend)
  request = friendship_with(friend)
  if request.try(:pending?)
    ActiveRecord::Base.transaction do
      [friendship_with(friend), friend.friendship_with(self)].compact.each do |friendship|
        friendship.deny! if friendship
      end
    end
    request.reload
    return request, Circle::Friendship::STATUS_FRIENDSHIP_DENIED
  else
    return nil, Circle::Friendship::STATUS_NOT_FOUND
  end
end

#friends?(friend) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/circle/circle.rb', line 65

def friends?(friend)
  friendship = friendship_with(friend)
  !!(friendship && friendship.accepted?)
end

#friendship_with(friend) ⇒ Object



61
62
63
# File 'lib/circle/circle.rb', line 61

def friendship_with(friend)
  friendships.where(friend_id: friend.id).first
end

#unblock(friend) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/circle/circle.rb', line 128

def unblock(friend)
  blocked_user = self.users_blocked.where(blocked_user_id: friend.id).first

  if blocked_user
    ActiveRecord::Base.transaction do
      blocked_user.destroy
    end
    return nil, Circle::Friendship::STATUS_UNBLOCKED
  else
    return nil, Circle::Friendship::STATUS_NOT_FOUND
  end
end

#unfriend(friend) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/circle/circle.rb', line 74

def unfriend(friend)
 ActiveRecord::Base.transaction do
    [friendship_with(friend), friend.friendship_with(self)].compact.each do |friendship|
      friendship.destroy if friendship
    end
  end
end