diff --git a/app/models/user.rb b/app/models/user.rb index 192dcdcf71a..0efcb78c84a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,6 +40,7 @@ class User < ActiveRecord::Base has_many :warnings has_many :user_archived_messages, dependent: :destroy has_many :email_change_requests, dependent: :destroy + has_many :directory_items, dependent: :delete_all has_one :user_option, dependent: :destroy diff --git a/spec/services/user_destroyer_spec.rb b/spec/services/user_destroyer_spec.rb index a31a8ffaaa3..55b94ff3fbd 100644 --- a/spec/services/user_destroyer_spec.rb +++ b/spec/services/user_destroyer_spec.rb @@ -82,7 +82,25 @@ describe UserDestroyer do UserDestroyer.new(admin).destroy(user) expect(QueuedPost.where(user_id: user.id).count).to eq(0) end + end + context "with a directory item record" do + let(:user) { Fabricate(:user) } + let(:admin) { Fabricate(:admin) } + + it "removes the directory item" do + DirectoryItem.create!( + user: user, + period_type: 1, + likes_received: 0, + likes_given: 0, + topics_entered: 0, + topic_count: 0, + post_count: 0 + ) + UserDestroyer.new(admin).destroy(user) + expect(DirectoryItem.where(user_id: user.id).count).to eq(0) + end end context "with a draft" do @@ -146,7 +164,7 @@ describe UserDestroyer do it "deletes topics started by the deleted user" do spammer_topic = Fabricate(:topic, user: @user) - spammer_post = Fabricate(:post, user: @user, topic: spammer_topic) + Fabricate(:post, user: @user, topic: spammer_topic) destroy expect(spammer_topic.reload.deleted_at).not_to eq(nil) expect(spammer_topic.user_id).to eq(nil)