Commit 06cb2c6531eed0113dfe95657b845fc3d5aab71c
1 parent
54a0ef94
Exists in
master
and in
27 other branches
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)
Showing
6 changed files
with
43 additions
and
21 deletions
Show diff stats
app/models/invitation.rb
... | ... | @@ -77,9 +77,9 @@ class Invitation < Task |
77 | 77 | |
78 | 78 | if !task_args.nil? |
79 | 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 | 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 | 83 | else |
84 | 84 | raise NotImplementedError, 'Don\'t know how to invite people to a %s' % profile.class.to_s |
85 | 85 | 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 < 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 < 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 < 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 < 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
... | ... | @@ -138,4 +138,16 @@ class InviteFriendTest < ActiveSupport::TestCase |
138 | 138 | assert_match(/#{task.requestor.name} wants to be your friend./, email.subject) |
139 | 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 | 153 | end | ... | ... |
test/unit/invite_member_test.rb
... | ... | @@ -154,4 +154,17 @@ class InviteMemberTest < ActiveSupport::TestCase |
154 | 154 | assert_match(/#{task.requestor.name} invited you to join #{task.community.name}/, email.subject) |
155 | 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 | 170 | end | ... | ... |