Module: HasFriendship::Friendable::InstanceMethods

Defined in:
lib/has_friendship/friendable.rb

Constant Summary collapse

CALLBACK_METHOD_NAMES =
%i(
  on_friendship_created
  on_friendship_accepted
  on_friendship_blocked
  on_friendship_destroyed
).freeze

Instance Method Summary collapse

Instance Method Details

#accept_request(friend) ⇒ Object



63
64
65
66
67
68
# File 'lib/has_friendship/friendable.rb', line 63

def accept_request(friend)
  on_relation_with(friend) do |one, other|
    friendship = HasFriendship::Friendship.find_unblocked_friendship(one, other)
    friendship.accept! if can_accept_request?(friendship)
  end
end

#block_friend(friend) ⇒ Object



78
79
80
81
82
# File 'lib/has_friendship/friendable.rb', line 78

def block_friend(friend)
  on_relation_with(friend) do |one, other|
    HasFriendship::Friendship.find_unblocked_friendship(one, other).block!
  end
end

#decline_request(friend) ⇒ Object Also known as: remove_friend



70
71
72
73
74
# File 'lib/has_friendship/friendable.rb', line 70

def decline_request(friend)
  on_relation_with(friend) do |one, other|
    HasFriendship::Friendship.find_unblocked_friendship(one, other).destroy
  end
end

#friend_request(friend) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/has_friendship/friendable.rb', line 54

def friend_request(friend)
  unless self == friend || HasFriendship::Friendship.exist?(self, friend)
    transaction do
      HasFriendship::Friendship.create_relation(self, friend, status: 0)
      HasFriendship::Friendship.create_relation(friend, self, status: 1)
    end
  end
end

#friends_with?(friend) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/has_friendship/friendable.rb', line 98

def friends_with?(friend)
  HasFriendship::Friendship.find_relation(self, friend, status: 2).any?
end

#on_relation_with(friend) ⇒ Object



91
92
93
94
95
96
# File 'lib/has_friendship/friendable.rb', line 91

def on_relation_with(friend)
  transaction do
    yield(self, friend)
    yield(friend, self)
  end
end

#unblock_friend(friend) ⇒ Object



84
85
86
87
88
89
# File 'lib/has_friendship/friendable.rb', line 84

def unblock_friend(friend)
  return unless has_blocked(friend)
  on_relation_with(friend) do |one, other|
    HasFriendship::Friendship.find_blocked_friendship(one, other).destroy
  end
end