]> source.dussan.org Git - redmine.git/commitdiff
Merged r13140 and r13142 (#16795).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 24 May 2014 16:37:38 +0000 (16:37 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 24 May 2014 16:37:38 +0000 (16:37 +0000)
git-svn-id: http://svn.redmine.org/redmine/branches/2.5-stable@13162 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/member.rb
test/unit/member_test.rb

index f8b87975e7e078fbfa14aecb2738f9a2ecc72ae4..6ec22d4223a994b290668dd7ab8b1a22f2d9200d 100644 (file)
@@ -73,6 +73,18 @@ class Member < ActiveRecord::Base
     member_roles.detect {|mr| mr.inherited_from}.nil?
   end
 
+  def destroy
+    if member_roles.reload.present?
+      # destroying the last role will destroy another instance
+      # of the same Member record, #super would then trigger callbacks twice
+      member_roles.destroy_all
+      @destroyed = true
+      freeze
+    else
+      super
+    end
+  end
+
   def include?(user)
     if principal.is_a?(Group)
       !user.nil? && user.groups.include?(principal)
index 586964b52e36f8daadbeb611a1ed6be6cd086315..6ba9717c464aba9a4acdca0923f9b3e7113f1f48 100644 (file)
@@ -107,6 +107,23 @@ class MemberTest < ActiveSupport::TestCase
     assert_nil category1.assigned_to_id
   end
 
+  def test_destroy_should_trigger_callbacks_only_once
+    Member.class_eval { def destroy_test_callback; end}
+    Member.after_destroy :destroy_test_callback
+
+    m = Member.create!(:user_id => 1, :project_id => 1, :role_ids => [1,3])
+
+    Member.any_instance.expects(:destroy_test_callback).once
+    assert_difference 'Member.count', -1 do
+      assert_difference 'MemberRole.count', -2 do
+        m.destroy
+      end
+    end
+    assert m.destroyed?
+  ensure
+    Member._destroy_callbacks.reject! {|c| c.filter==:destroy_test_callback}
+  end
+
   def test_sort_without_roles
     a = Member.new(:roles => [Role.first])
     b = Member.new