Skip to content

Commit

Permalink
Add deny_friendship_with
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxlli committed Apr 28, 2009
1 parent 8739a75 commit 5741a7b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
class YouCanNotAcceptARequestFriendshipError < StandardError; end
class YouCanNotJudgeARequestFriendshipError < StandardError; end
33 changes: 19 additions & 14 deletions lib/friendship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Friendship < ActiveRecord::Base

# callback
after_destroy do |f|
User.decrement_counter(:friends_count, f.user_id)
User.decrement_counter(:friends_count, f.user_id) if f.status == FRIENDSHIP_ACCEPTED
end

def pending?
Expand All @@ -40,23 +40,28 @@ def accepted?
def requested?
status == FRIENDSHIP_REQUESTED
end
def accept!(new_relations = nil)

def accept!(new_relation_names = nil)
unless accepted?
User.increment_counter(:friends_count, user.id)
update_attribute(:status, FRIENDSHIP_ACCEPTED)
add_relations(new_relations) unless new_relations.nil?
self.transaction do
User.increment_counter(:friends_count, user.id)
update_attribute(:status, FRIENDSHIP_ACCEPTED)
add_relations(new_relation_names) unless new_relation_names.nil?
end
end
end

def add_relations(new_relations = [])
self.relations.each do |r|
r.destroy unless new_relations.include?(r.name.to_sym)
end

new_relations.each do |r|
relation = RelationType.find_or_create_by_name(r.to_s)
self.relations << relation unless self.relations.include?(relation)
def add_relations(new_relation_names = [])
self.transaction do
actual_relation_names = self.relation_names
self.friendship_relations.each do |fr|
fr.destroy unless actual_relation_names.include?(fr.relation.name.to_sym)
end

new_relation_names.each do |nr|
relation = RelationType.find_or_create_by_name(nr.to_s)
self.relations << relation unless relations.include?(relation)
end
end
end

Expand Down
14 changes: 13 additions & 1 deletion lib/has_friends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def remove_friendship_with(friend)
end
end

def deny_friendship_with(friend)
if (pendent = self.friendship_for(friend)).pending?
ActiveRecord::Base.transaction do
[friendship_for(friend), friend.friendship_for(self)].compact.each do |friendship|
friendship.destroy if friendship
end
end
else
raise YouCanNotJudgeARequestFriendshipError
end
end

def accept_friendship_with(friend, relations = nil)
if (pendent = self.friendship_for(friend)).pending?
requested = friend.friendship_for(self)
Expand All @@ -90,7 +102,7 @@ def accept_friendship_with(friend, relations = nil)
requested.accept! unless requested.accepted?
end
else
raise YouCanNotAcceptARequestFriendshipError
raise YouCanNotJudgeARequestFriendshipError
end
end

Expand Down
50 changes: 38 additions & 12 deletions spec/has_friends_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,62 @@
before(:each) do
#requesting friendship between @vader and @luke
@vader.be_friends_with(@luke)
@leia.be_friends_with(@luke)
@luke.accept_friendship_with(@leia)
end

it "should accept a friendship" do
@luke.accept_friendship_with(@vader)

@luke.reload
@vader.reload

@vader.friends.should == [@luke]
@luke.friends.should == [@vader]
@luke.friends.should == [@vader, @leia]
@vader.friends_count.should == 1
@luke.friends_count.should == 1
@luke.friends_count.should == 2
end

it "should accept a friendship with relation types" do
doing {
@luke.accept_friendship_with(@vader, [:parent, :met])
}.should change(FriendshipRelationType, :count).by(2)
end

it "should reject when user try accept his friendship request" do
lambda { @vader.accept_friendship_with(@luke) }.should raise_error(YouCanNotAcceptARequestFriendshipError)
lambda { @vader.accept_friendship_with(@luke) }.should raise_error(YouCanNotJudgeARequestFriendshipError)

@luke.reload
@vader.reload

@vader.friends.should == []
@luke.friends.should == []
@luke.friends.should == [@leia]
@vader.friends_count.should == 0
@luke.friends_count.should == 0
@luke.friends_count.should == 1
end

it "should accept a friendship with relation types" do
@luke.deny_friendship_with(@vader)

@luke.reload
@vader.reload

@vader.friends.should == []
@luke.friends.should == [@leia]
@vader.friends_count.should == 0
@luke.friends_count.should == 1
end

it "should reject when user try deny his friendship request" do
lambda { @vader.deny_friendship_with(@luke) }.should raise_error(YouCanNotJudgeARequestFriendshipError)

@luke.reload
@vader.reload

@vader.friends.should == []
@luke.friends.should == [@leia]
@vader.friends_count.should == 0
@luke.friends_count.should == 1
end

it "should remove a friendship" do
Expand All @@ -71,9 +97,9 @@
@vader.reload

@vader.friends.should == []
@luke.friends.should == []
@luke.friends.should == [@leia]
@vader.friends_count.should == 0
@luke.friends_count.should == 0
@luke.friends_count.should == 1
end
end

Expand Down

0 comments on commit 5741a7b

Please sign in to comment.