Commit 06cb2c6531eed0113dfe95657b845fc3d5aab71c

Authored by Larissa Reis
1 parent 54a0ef94

invite-members: encapsulates logic for check of existence of task

  Refactoring of avoiding duplicate invite task. Check existence of task
  is now encapsulated inside Invite

(ActionItem3099)
app/models/invitation.rb
@@ -77,9 +77,9 @@ class Invitation < Task @@ -77,9 +77,9 @@ class Invitation < Task
77 77
78 if !task_args.nil? 78 if !task_args.nil?
79 if profile.person? 79 if profile.person?
80 - InviteFriend.create(task_args) if !user || user.person.tasks.pending.of("InviteFriend").find(:all, :conditions => {:requestor_id => person.id, :target_id => user.person.id}).blank? 80 + InviteFriend.create(task_args)
81 elsif profile.community? 81 elsif profile.community?
82 - InviteMember.create(task_args.merge(:community_id => profile.id)) if !user || user.person.tasks.pending.of("InviteMember").find(:all, :conditions => {:requestor_id => person.id}).select { |t| t.data[:community_id] == profile.id }.blank? 82 + InviteMember.create(task_args.merge(:community_id => profile.id))
83 else 83 else
84 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s 84 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s
85 end 85 end
app/models/invite_friend.rb
1 class InviteFriend < Invitation 1 class InviteFriend < Invitation
2 2
3 settings_items :group_for_person, :group_for_friend 3 settings_items :group_for_person, :group_for_friend
  4 + before_create :check_for_invitation_existence
4 5
5 def perform 6 def perform
6 person.add_friend(friend, group_for_person) 7 person.add_friend(friend, group_for_person)
@@ -41,4 +42,11 @@ class InviteFriend &lt; Invitation @@ -41,4 +42,11 @@ class InviteFriend &lt; Invitation
41 ].join("\n\n") 42 ].join("\n\n")
42 end 43 end
43 44
  45 + private
  46 + def check_for_invitation_existence
  47 + if friend
  48 + friend.tasks.pending.of("InviteFriend").find(:all, :conditions => {:requestor_id => person.id, :target_id => friend.id}).blank?
  49 + end
  50 + end
  51 +
44 end 52 end
app/models/invite_member.rb
@@ -2,6 +2,7 @@ class InviteMember &lt; Invitation @@ -2,6 +2,7 @@ class InviteMember &lt; Invitation
2 2
3 settings_items :community_id, :type => :integer 3 settings_items :community_id, :type => :integer
4 validates_presence_of :community_id 4 validates_presence_of :community_id
  5 + before_create :check_for_invitation_existence
5 6
6 def community 7 def community
7 Community.find(community_id) 8 Community.find(community_id)
@@ -61,4 +62,11 @@ class InviteMember &lt; Invitation @@ -61,4 +62,11 @@ class InviteMember &lt; Invitation
61 ].join("\n\n") 62 ].join("\n\n")
62 end 63 end
63 64
  65 + private
  66 + def check_for_invitation_existence
  67 + if friend
  68 + friend.tasks.pending.of("InviteMember").find(:all, :conditions => {:requestor_id => person.id}).select { |t| t.data[:community_id] == community_id }.blank?
  69 + end
  70 + end
  71 +
64 end 72 end
test/unit/invitation_test.rb
@@ -134,23 +134,4 @@ class InvitationTest &lt; ActiveSupport::TestCase @@ -134,23 +134,4 @@ class InvitationTest &lt; ActiveSupport::TestCase
134 end 134 end
135 end 135 end
136 136
137 - should 'not invite friends if there is a pending invitation' do  
138 - person = create_user('testuser1').person  
139 - friend = create_user('testuser2').person  
140 - community = fast_create(Community)  
141 -  
142 - assert_difference InviteMember, :count do  
143 - Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)  
144 - end  
145 - assert_difference InviteFriend, :count do  
146 - Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)  
147 - end  
148 -  
149 - assert_no_difference InviteMember, :count do  
150 - Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', community)  
151 - end  
152 - assert_no_difference InviteFriend, :count do  
153 - Invitation.invite(person, [friend.id.to_s], 'hello friend <url>', person)  
154 - end  
155 - end  
156 end 137 end
test/unit/invite_friend_test.rb
@@ -138,4 +138,16 @@ class InviteFriendTest &lt; ActiveSupport::TestCase @@ -138,4 +138,16 @@ class InviteFriendTest &lt; ActiveSupport::TestCase
138 assert_match(/#{task.requestor.name} wants to be your friend./, email.subject) 138 assert_match(/#{task.requestor.name} wants to be your friend./, email.subject)
139 end 139 end
140 140
  141 + should 'not invite friends if there is a pending invitation' do
  142 + person = create_user('testuser1').person
  143 + friend = create_user('testuser2').person
  144 +
  145 + assert_difference 'InviteFriend.count' do
  146 + InviteFriend.create({:person => person, :target => friend})
  147 + end
  148 +
  149 + assert_no_difference 'InviteFriend.count' do
  150 + InviteFriend.create({:person => person, :target => friend})
  151 + end
  152 + end
141 end 153 end
test/unit/invite_member_test.rb
@@ -154,4 +154,17 @@ class InviteMemberTest &lt; ActiveSupport::TestCase @@ -154,4 +154,17 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
154 assert_match(/#{task.requestor.name} invited you to join #{task.community.name}/, email.subject) 154 assert_match(/#{task.requestor.name} invited you to join #{task.community.name}/, email.subject)
155 end 155 end
156 156
  157 + should 'not invite member if there is a pending invitation' do
  158 + person = create_user('testuser1').person
  159 + friend = create_user('testuser2').person
  160 + community = fast_create(Community)
  161 +
  162 + assert_difference 'InviteMember.count' do
  163 + InviteMember.create({:person => person, :target => friend, :community_id => community.id})
  164 + end
  165 +
  166 + assert_no_difference 'InviteMember.count' do
  167 + InviteMember.create({:person => person, :target => friend, :community_id => community.id})
  168 + end
  169 + end
157 end 170 end