Commit d69788f1165d1042b74cff3f16ed8a51f1e8cb80

Authored by Larissa Reis
1 parent 8c86f904

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
... ... @@ -78,9 +78,9 @@ class Invitation < Task
78 78  
79 79 if !task_args.nil?
80 80 if profile.person?
81   - 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?
  81 + InviteFriend.create(task_args)
82 82 elsif profile.community?
83   - 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?
  83 + InviteMember.create(task_args.merge(:community_id => profile.id))
84 84 else
85 85 raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s
86 86 end
... ...
app/models/invite_friend.rb
1 1 class InviteFriend < Invitation
2 2  
3 3 settings_items :group_for_person, :group_for_friend
  4 + before_create :check_for_invitation_existence
4 5  
5 6 def perform
6 7 person.add_friend(friend, group_for_person)
... ... @@ -41,4 +42,11 @@ class InviteFriend &lt; Invitation
41 42 ].join("\n\n")
42 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 52 end
... ...
app/models/invite_member.rb
... ... @@ -2,6 +2,7 @@ class InviteMember &lt; Invitation
2 2  
3 3 settings_items :community_id, :type => :integer
4 4 validates_presence_of :community_id
  5 + before_create :check_for_invitation_existence
5 6  
6 7 def community
7 8 Community.find(community_id)
... ... @@ -61,4 +62,11 @@ class InviteMember &lt; Invitation
61 62 ].join("\n\n")
62 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 72 end
... ...
test/unit/invitation_test.rb
... ... @@ -134,23 +134,4 @@ class InvitationTest &lt; ActiveSupport::TestCase
134 134 end
135 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 137 end
... ...
test/unit/invite_friend_test.rb
... ... @@ -140,4 +140,16 @@ class InviteFriendTest &lt; ActiveSupport::TestCase
140 140 assert_match(/#{task.requestor.name} wants to be your friend./, email.subject)
141 141 end
142 142  
  143 + should 'not invite friends if there is a pending invitation' do
  144 + person = create_user('testuser1').person
  145 + friend = create_user('testuser2').person
  146 +
  147 + assert_difference 'InviteFriend.count' do
  148 + InviteFriend.create({:person => person, :target => friend})
  149 + end
  150 +
  151 + assert_no_difference 'InviteFriend.count' do
  152 + InviteFriend.create({:person => person, :target => friend})
  153 + end
  154 + end
143 155 end
... ...
test/unit/invite_member_test.rb
... ... @@ -171,4 +171,18 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
171 171 email = TaskMailer.target_notification(task, task.target_notification_message).deliver
172 172 assert_match(/#{task.requestor.name} invited you to join #{task.community.name}/, email.subject)
173 173 end
  174 +
  175 + should 'not invite member if there is a pending invitation' do
  176 + person = create_user('testuser1').person
  177 + friend = create_user('testuser2').person
  178 + community = fast_create(Community)
  179 +
  180 + assert_difference 'InviteMember.count' do
  181 + InviteMember.create({:person => person, :target => friend, :community_id => community.id})
  182 + end
  183 +
  184 + assert_no_difference 'InviteMember.count' do
  185 + InviteMember.create({:person => person, :target => friend, :community_id => community.id})
  186 + end
  187 + end
174 188 end
... ...