invite sends friend request if already friends, and raises if already friends

This commit is contained in:
ilya 2010-10-18 16:26:56 -07:00
parent 7ab2b00231
commit d642928268
4 changed files with 31 additions and 2 deletions

View file

@ -19,7 +19,8 @@ class InvitationsController < Devise::InvitationsController
flash[:error] = I18n.t 'invitations.create.no_more'
elsif e.message == "You already invited this person"
flash[:error] = I18n.t 'invitations.create.already_sent'
elsif e.message == "You are already friends with this person"
flash[:error] = I18n.t 'invitations.create.already_friends'
else
raise e
end

View file

@ -290,8 +290,21 @@ class User
aspect_id = opts.delete(:aspect_id)
if aspect_id == nil
raise "Must invite into aspect"
elsif !(self.aspects.find_by_id(aspect_id))
end
aspect_object = self.aspects.find_by_id(aspect_id)
if !(aspect_object)
raise "Must invite to your aspect"
else
u = User.find_by_email(opts[:email])
if u.nil?
elsif friends.include?(u.person)
raise "You are already friends with this person"
elsif not u.invited?
self.send_friend_request_to(u.person, aspect_object)
return
elsif u.invited? && u.inviters.include?(self)
raise "You already invited this person"
end
end
request = Request.instantiate(
:to => "http://local_request.example.com",

View file

@ -172,6 +172,7 @@ en:
sent: 'Your invitation has been sent.'
no_more: 'You have no more invitations.'
already_sent: 'You already invited this person.'
already_friends: 'You are already friends with this person'
invitation_token_invalid: 'The invitation token provided is not valid!'
updated: 'Your password was set successfully. You are now signed in.'

View file

@ -64,6 +64,20 @@ describe User do
inviter.reload
inviter.pending_requests.find_by_callback_url(inviter.receive_url).nil?.should == false
end
it 'throws if you try to add someone you"re friends with' do
friend_users(inviter, aspect, another_user, wrong_aspect)
inviter.reload
proc{inviter.invite_user(:email => another_user.email, :aspect_id => aspect.id)}.should raise_error /You are already friends with this person/
end
it 'sends a friend request to a user with that email into the aspect' do
inviter.should_receive(:send_friend_request_to){ |a, b|
a.should == another_user.person
b.should == aspect
}
inviter.invite_user(:email => another_user.email, :aspect_id => aspect.id)
end
end
context "limit on invites" do