Commit a77dc98f6682ae45ca58e786cd864584f6218043
1 parent
67904292
Exists in
master
and in
22 other branches
ActionItem406: dont add friend twice
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1903 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
7 changed files
with
38 additions
and
1 deletions
Show diff stats
app/controllers/my_profile/friends_controller.rb
| @@ -9,6 +9,7 @@ class FriendsController < MyProfileController | @@ -9,6 +9,7 @@ class FriendsController < MyProfileController | ||
| 9 | def add | 9 | def add |
| 10 | @friend = Person.find(params[:id]) | 10 | @friend = Person.find(params[:id]) |
| 11 | if request.post? && params[:confirmation] | 11 | if request.post? && params[:confirmation] |
| 12 | + # FIXME this shouldn't be in Person model? | ||
| 12 | AddFriend.create!(:person => profile, :friend => @friend, :group_for_person => params[:group]) | 13 | AddFriend.create!(:person => profile, :friend => @friend, :group_for_person => params[:group]) |
| 13 | 14 | ||
| 14 | flash[:notice] = _('%s still needs to accept being your friend.') % @friend.name | 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,6 +4,8 @@ class AddFriend < Task | ||
| 4 | 4 | ||
| 5 | validates_presence_of :requestor_id, :target_id | 5 | validates_presence_of :requestor_id, :target_id |
| 6 | 6 | ||
| 7 | + validates_uniqueness_of :target_id, :scope => [ :requestor_id ] | ||
| 8 | + | ||
| 7 | alias :person :requestor | 9 | alias :person :requestor |
| 8 | alias :person= :requestor= | 10 | alias :person= :requestor= |
| 9 | 11 |
app/models/person.rb
| @@ -18,6 +18,10 @@ class Person < Profile | @@ -18,6 +18,10 @@ class Person < Profile | ||
| 18 | self.friendships.build(:friend => friend, :group => group).save! | 18 | self.friendships.build(:friend => friend, :group => group).save! |
| 19 | end | 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 | def remove_friend(friend) | 25 | def remove_friend(friend) |
| 22 | friends.delete(friend) | 26 | friends.delete(friend) |
| 23 | end | 27 | end |
app/views/blocks/profile_info_actions/person.rhtml
| 1 | <ul> | 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 | <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> | 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 | <% end %> | 4 | <% end %> |
| 5 | </ul> | 5 | </ul> |
test/functional/profile_controller_test.rb
| @@ -193,4 +193,18 @@ class ProfileControllerTest < Test::Unit::TestCase | @@ -193,4 +193,18 @@ class ProfileControllerTest < Test::Unit::TestCase | ||
| 193 | assert_response 403 | 193 | assert_response 403 |
| 194 | end | 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 | end | 210 | end |
test/unit/add_friend_test.rb
| @@ -87,4 +87,13 @@ class AddFriendTest < ActiveSupport::TestCase | @@ -87,4 +87,13 @@ class AddFriendTest < ActiveSupport::TestCase | ||
| 87 | assert_equal :manage_friends, t.permission | 87 | assert_equal :manage_friends, t.permission |
| 88 | end | 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 | end | 99 | end |
test/unit/person_test.rb
| @@ -267,4 +267,11 @@ class PersonTest < Test::Unit::TestCase | @@ -267,4 +267,11 @@ class PersonTest < Test::Unit::TestCase | ||
| 267 | assert person.errors.invalid?(:name) | 267 | assert person.errors.invalid?(:name) |
| 268 | end | 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 | end | 277 | end |