diff --git a/app/models/user.rb b/app/models/user.rb index 4ee8ead2..0439f237 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index b2e45ff8..a7122c0c 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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