Commit 1761e6e43101676fe34f9e7f1138fb446e177044

Authored by Junior Silva
1 parent 27818a77

invite-member: fixed inviteMember crash when community is destroyed

AI3054
app/models/community.rb
1 class Community < Organization 1 class Community < Organization
2 2
  3 + after_destroy :check_invite_member_for_destroy
  4 +
3 def self.type_name 5 def self.type_name
4 _('Community') 6 _('Community')
5 end 7 end
@@ -17,6 +19,10 @@ class Community &lt; Organization @@ -17,6 +19,10 @@ class Community &lt; Organization
17 community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default') 19 community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default')
18 end 20 end
19 21
  22 + def check_invite_member_for_destroy
  23 + InviteMember.pending.select { |task| task.community_id == self.id }.map(&:destroy)
  24 + end
  25 +
20 def self.create_after_moderation(requestor, attributes = {}) 26 def self.create_after_moderation(requestor, attributes = {})
21 community = Community.new(attributes) 27 community = Community.new(attributes)
22 if community.environment.enabled?('admin_must_approve_new_communities') 28 if community.environment.enabled?('admin_must_approve_new_communities')
test/unit/invite_member_test.rb
@@ -118,13 +118,16 @@ class InviteMemberTest &lt; ActiveSupport::TestCase @@ -118,13 +118,16 @@ class InviteMemberTest &lt; ActiveSupport::TestCase
118 should 'destroy InviteMember task when the community is destroyed' do 118 should 'destroy InviteMember task when the community is destroyed' do
119 p1 = create_user('testuser1').person 119 p1 = create_user('testuser1').person
120 p2 = create_user('testuser2').person 120 p2 = create_user('testuser2').person
  121 + p3 = create_user('testuser3').person
121 community = fast_create(Community) 122 community = fast_create(Community)
122 123
123 - task = InviteMember.create!(:person => p1, :friend => p2, :friend_email => 'test@test.com', :community_id => community.id) 124 + t1 = InviteMember.create!(:person => p1, :friend => p2, :community_id => community.id)
  125 + t2 = InviteMember.create!(:person => p1, :friend => p3, :community_id => community.id)
124 community.destroy 126 community.destroy
125 127
126 - assert_nil task.reload  
127 - end 128 + assert_raise ActiveRecord::RecordNotFound do; t1.reload; end
  129 + assert_raise ActiveRecord::RecordNotFound do; t2.reload; end
  130 + end
128 131
129 end 132 end
130 133