Skip to content

Commit

Permalink
Fix friends_with to return only true when friendship is accepted. Fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
Sung Won Cho committed Mar 5, 2016
1 parent e19a851 commit f73d1ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/has_friendship/friendable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def on_relation_with(friend)
end

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

private
Expand Down
14 changes: 10 additions & 4 deletions lib/has_friendship/friendship.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module HasFriendship
class Friendship < ActiveRecord::Base
def self.relation_attributes(one, other)
{
def self.relation_attributes(one, other, status: nil)
attr = {
friendable_id: one.id,
friendable_type: one.class.base_class.name,
friend_id: other.id
}

if status
attr[:status] = status
end

attr
end

def self.create_relation(one, other, options)
Expand All @@ -14,8 +20,8 @@ def self.create_relation(one, other, options)
relation.save
end

def self.find_relation(friendable, friend)
where relation_attributes(friendable, friend)
def self.find_relation(friendable, friend, status: nil)
where relation_attributes(friendable, friend, status: status)
end

def self.exist?(friendable, friend)
Expand Down
14 changes: 14 additions & 0 deletions spec/has_friendship/friendable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@
expect(jon.friends_with?(user)).to eq false
end
end

context 'when a pending friendship exists' do
it 'returns false' do
create_friendship(user, friend, status: 'pending')
expect(user.friends_with?(friend)).to eq false
end
end

context 'when a blocked friendship exists' do
it 'returns false' do
create_friendship(user, friend, status: 'blocked')
expect(user.friends_with?(friend)).to eq false
end
end
end
end
end

0 comments on commit f73d1ef

Please sign in to comment.