Module: RailsFriends::Friendable::InstanceMethods

Defined in:
lib/rails_friends/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/rails_friends/friendable.rb', line 63

def accept_request(friend)
  on_relation_with(friend) do |one, other|
    friendship = RailsFriends::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/rails_friends/friendable.rb', line 78

def block_friend(friend)
  on_relation_with(friend) do |one, other|
    RailsFriends::Friendship.find_unblocked_friendship(one, other).block_by(self)
  end
end

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



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

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

#friend_request(friend) ⇒ Object



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

def friend_request(friend)
  return if self == friend || RailsFriends::Friendship.exist?(self, friend)

  transaction do
    RailsFriends::Friendship.create_relation(self, friend, status: 0)
    RailsFriends::Friendship.create_relation(friend, self, status: 1)
  end
end

#friends_with?(friend) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rails_friends/friendable.rb', line 99

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

#on_relation_with(friend) ⇒ Object



92
93
94
95
96
97
# File 'lib/rails_friends/friendable.rb', line 92

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
90
# File 'lib/rails_friends/friendable.rb', line 84

def unblock_friend(friend)
  return unless has_blocked(friend)

  on_relation_with(friend) do |one, other|
    RailsFriends::Friendship.find_blocked_friendship(one, other).destroy
  end
end