fix #1400 where deleting a user will clean up tags and dependencies too

This commit is contained in:
Reinier Balt 2014-09-23 16:35:45 +02:00
parent f8d4f85a8c
commit 18b7a467c1
2 changed files with 16 additions and 1 deletions

View file

@ -108,6 +108,7 @@ class User < ActiveRecord::Base
before_create :crypt_password, :generate_token
before_update :crypt_password
before_destroy :destroy_dependencies, :delete_taggings, prepend: true # run before deleting todos, projects, contexts, etc.
def validate_auth_type
unless Tracks::Config.auth_schemes.include?(auth_type)
@ -220,4 +221,16 @@ protected
auth_type == 'database' && crypted_password.blank? || password.present?
end
def destroy_dependencies
ids = todos.pluck(:id)
pred_deps = Dependency.where(predecessor_id: ids).destroy_all
succ_deps = Dependency.where(predecessor_id: ids).destroy_all
end
def delete_taggings
ids = todos.pluck(:id)
taggings = Tagging.where(taggable_id: ids).pluck(:id)
Tagging.where(id: taggings).delete_all
end
end

View file

@ -393,12 +393,14 @@ class UserTest < ActiveSupport::TestCase
nr_of_contexts = u.contexts.count
nr_of_rec_todos = u.recurring_todos.count
nr_of_notes = u.notes.count
nr_of_deps = (Dependency.where(predecessor_id: u.todos.pluck(:id)).pluck(:id) + Dependency.where(successor_id: u.todos.pluck(:id)).pluck(:id)).uniq.count
expect_todos_count = Todo.count - nr_of_todos
expect_projects_count = Project.count - nr_of_projects
expect_contexts_count = Context.count - nr_of_contexts
expect_rec_todos_count = RecurringTodo.count - nr_of_rec_todos
expect_notes_count = Note.count - nr_of_notes
expect_deps_count = Dependency.count - nr_of_deps
u.destroy
@ -407,7 +409,7 @@ class UserTest < ActiveSupport::TestCase
assert_equal expect_contexts_count, Context.count, "expected #{nr_of_contexts} contexts to be gone"
assert_equal expect_rec_todos_count, RecurringTodo.count, "expected #{nr_of_rec_todos} repeating todos to be gone"
assert_equal expect_notes_count, Note.count, "expected #{nr_of_notes} notes to be gone"
assert_equal expect_deps_count, Dependency.count, "expected #{nr_of_deps} dependencies to be gone"
end
protected