Commit a77dc98f6682ae45ca58e786cd864584f6218043

Authored by JoenioCosta
1 parent 67904292

ActionItem406: dont add friend twice


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1903 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/friends_controller.rb
... ... @@ -9,6 +9,7 @@ class FriendsController < MyProfileController
9 9 def add
10 10 @friend = Person.find(params[:id])
11 11 if request.post? && params[:confirmation]
  12 + # FIXME this shouldn't be in Person model?
12 13 AddFriend.create!(:person => profile, :friend => @friend, :group_for_person => params[:group])
13 14  
14 15 flash[:notice] = _('%s still needs to accept being your friend.') % @friend.name
... ...
app/models/add_friend.rb
... ... @@ -4,6 +4,8 @@ class AddFriend < Task
4 4  
5 5 validates_presence_of :requestor_id, :target_id
6 6  
  7 + validates_uniqueness_of :target_id, :scope => [ :requestor_id ]
  8 +
7 9 alias :person :requestor
8 10 alias :person= :requestor=
9 11  
... ...
app/models/person.rb
... ... @@ -18,6 +18,10 @@ class Person < Profile
18 18 self.friendships.build(:friend => friend, :group => group).save!
19 19 end
20 20  
  21 + def already_request_friendship?(person)
  22 + person.tasks.find_by_requestor_id(self.id, :conditions => { :type => 'AddFriend' })
  23 + end
  24 +
21 25 def remove_friend(friend)
22 26 friends.delete(friend)
23 27 end
... ...
app/views/blocks/profile_info_actions/person.rhtml
1 1 <ul>
2   - <%if logged_in? && (user != profile) && (! user.friends.include?(profile)) %>
  2 + <%if logged_in? && (user != profile) && (! user.friends.include?(profile)) && !user.already_request_friendship?(profile) %>
3 3 <li><%= link_to content_tag('span', _('Add friend')), { :profile => user.identifier, :controller => 'friends', :action => 'add', :id => profile.id }, :class => 'button with-text icon-add' %></li>
4 4 <% end %>
5 5 </ul>
... ...
test/functional/profile_controller_test.rb
... ... @@ -193,4 +193,18 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
193 193 assert_response 403
194 194 end
195 195  
  196 + should 'display add friend button' do
  197 + login_as(@profile.identifier)
  198 + friend = create_user('friendtestuser').person
  199 + get :index, :profile => friend.identifier
  200 + assert_tag :tag => 'a', :content => 'Add friend'
  201 + end
  202 +
  203 + should 'not display add friend button if user already request friendship' do
  204 + friend = create_user('friendtestuser').person
  205 + AddFriend.create!(:person => @profile, :friend => friend)
  206 + get :index, :profile => friend.identifier
  207 + assert_no_tag :tag => 'a', :content => 'Add friend'
  208 + end
  209 +
196 210 end
... ...
test/unit/add_friend_test.rb
... ... @@ -87,4 +87,13 @@ class AddFriendTest &lt; ActiveSupport::TestCase
87 87 assert_equal :manage_friends, t.permission
88 88 end
89 89  
  90 + should 'not add friend twice' do
  91 + p1 = create_user('testuser1').person
  92 + p2 = create_user('testuser2').person
  93 + AddFriend.create!(:person => p1, :friend => p2)
  94 + assert_raise ActiveRecord::RecordInvalid do
  95 + AddFriend.create!(:person => p1, :friend => p2)
  96 + end
  97 + end
  98 +
90 99 end
... ...
test/unit/person_test.rb
... ... @@ -267,4 +267,11 @@ class PersonTest &lt; Test::Unit::TestCase
267 267 assert person.errors.invalid?(:name)
268 268 end
269 269  
  270 + should 'already request friendship' do
  271 + p1 = create_user('testuser1').person
  272 + p2 = create_user('testuser2').person
  273 + AddFriend.create!(:person => p1, :friend => p2)
  274 + assert p1.already_request_friendship?(p2)
  275 + end
  276 +
270 277 end
... ...