diff --git a/app/controllers/my_profile/friends_controller.rb b/app/controllers/my_profile/friends_controller.rb index effcc9c..67b804c 100644 --- a/app/controllers/my_profile/friends_controller.rb +++ b/app/controllers/my_profile/friends_controller.rb @@ -9,6 +9,7 @@ class FriendsController < MyProfileController def add @friend = Person.find(params[:id]) if request.post? && params[:confirmation] + # FIXME this shouldn't be in Person model? AddFriend.create!(:person => profile, :friend => @friend, :group_for_person => params[:group]) flash[:notice] = _('%s still needs to accept being your friend.') % @friend.name diff --git a/app/models/add_friend.rb b/app/models/add_friend.rb index 5f33b61..83e253a 100644 --- a/app/models/add_friend.rb +++ b/app/models/add_friend.rb @@ -4,6 +4,8 @@ class AddFriend < Task validates_presence_of :requestor_id, :target_id + validates_uniqueness_of :target_id, :scope => [ :requestor_id ] + alias :person :requestor alias :person= :requestor= diff --git a/app/models/person.rb b/app/models/person.rb index 41accc1..c0b9edb 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -18,6 +18,10 @@ class Person < Profile self.friendships.build(:friend => friend, :group => group).save! end + def already_request_friendship?(person) + person.tasks.find_by_requestor_id(self.id, :conditions => { :type => 'AddFriend' }) + end + def remove_friend(friend) friends.delete(friend) end diff --git a/app/views/blocks/profile_info_actions/person.rhtml b/app/views/blocks/profile_info_actions/person.rhtml index f024e29..17308e1 100644 --- a/app/views/blocks/profile_info_actions/person.rhtml +++ b/app/views/blocks/profile_info_actions/person.rhtml @@ -1,5 +1,5 @@ diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb index 8e705e9..a9da92a 100644 --- a/test/functional/profile_controller_test.rb +++ b/test/functional/profile_controller_test.rb @@ -193,4 +193,18 @@ class ProfileControllerTest < Test::Unit::TestCase assert_response 403 end + should 'display add friend button' do + login_as(@profile.identifier) + friend = create_user('friendtestuser').person + get :index, :profile => friend.identifier + assert_tag :tag => 'a', :content => 'Add friend' + end + + should 'not display add friend button if user already request friendship' do + friend = create_user('friendtestuser').person + AddFriend.create!(:person => @profile, :friend => friend) + get :index, :profile => friend.identifier + assert_no_tag :tag => 'a', :content => 'Add friend' + end + end diff --git a/test/unit/add_friend_test.rb b/test/unit/add_friend_test.rb index c5bc5dc..5c1a1fc 100644 --- a/test/unit/add_friend_test.rb +++ b/test/unit/add_friend_test.rb @@ -87,4 +87,13 @@ class AddFriendTest < ActiveSupport::TestCase assert_equal :manage_friends, t.permission end + should 'not add friend twice' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + AddFriend.create!(:person => p1, :friend => p2) + assert_raise ActiveRecord::RecordInvalid do + AddFriend.create!(:person => p1, :friend => p2) + end + end + end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 1905df3..882adcb 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -267,4 +267,11 @@ class PersonTest < Test::Unit::TestCase assert person.errors.invalid?(:name) end + should 'already request friendship' do + p1 = create_user('testuser1').person + p2 = create_user('testuser2').person + AddFriend.create!(:person => p1, :friend => p2) + assert p1.already_request_friendship?(p2) + end + end -- libgit2 0.21.2