diff --git a/app/models/post.rb b/app/models/post.rb index dda9485194b..90174c2f612 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -25,6 +25,7 @@ class Post < ActiveRecord::Base has_many :post_replies has_many :replies, through: :post_replies has_many :post_actions + has_many :topic_links has_and_belongs_to_many :upload @@ -52,9 +53,15 @@ class Post < ActiveRecord::Base @types ||= Enum.new(:regular, :moderator_action) end + def trash! + self.topic_links.each(&:destroy) + super + end + def recover! super update_flagged_posts_count + TopicLink.extract_from(self) end # The key we use in redis to ensure unique posts diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index 539cc9c5201..b4ada546cf7 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -13,7 +13,7 @@ class TopicLink < ActiveRecord::Base validates_uniqueness_of :url, scope: [:topic_id, :post_id] - has_many :topic_link_clicks + has_many :topic_link_clicks, dependent: :destroy validate :link_to_self diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index dd89b272811..f39ad7caf81 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -69,6 +69,7 @@ class PostDestroyer @post.revise(@user, I18n.t('js.post.deleted_by_author'), force_new_version: true) @post.update_column(:user_deleted, true) @post.update_flagged_posts_count + @post.topic_links.each(&:destroy) end end diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb index b18edb7d69b..357edf0e9e0 100644 --- a/spec/components/post_destroyer_spec.rb +++ b/spec/components/post_destroyer_spec.rb @@ -166,5 +166,23 @@ describe PostDestroyer do end end + describe 'topic links' do + let!(:first_post) { Fabricate(:post) } + let!(:topic) { first_post.topic } + let!(:second_post) { Fabricate(:post_with_external_links, topic: topic) } + + before { TopicLink.extract_from(second_post) } + + it 'should destroy the topic links when moderator destroys the post' do + PostDestroyer.new(moderator, second_post.reload).destroy + expect(topic.topic_links.count).to eq(0) + end + + it 'should destroy the topic links when the user destroys the post' do + PostDestroyer.new(second_post.user, second_post.reload).destroy + expect(topic.topic_links.count).to eq(0) + end + end + end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 2f5a8da5b91..b80768174c0 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -56,26 +56,44 @@ describe Post do end describe "versions and deleting/recovery" do - let(:post) { Fabricate(:post, post_args) } - before do - post.trash! - post.reload - end + context 'a post without links' do + let(:post) { Fabricate(:post, post_args) } - it "doesn't create a new version when deleted" do - post.versions.count.should == 0 - end - - describe "recovery" do before do - post.recover! + post.trash! post.reload end - it "doesn't create a new version when recovered" do + it "doesn't create a new version when deleted" do post.versions.count.should == 0 end + + describe "recovery" do + before do + post.recover! + post.reload + end + + it "doesn't create a new version when recovered" do + post.versions.count.should == 0 + end + end + end + + context 'a post with links' do + let(:post) { Fabricate(:post_with_external_links) } + before do + post.trash! + post.reload + end + + describe 'recovery' do + it 'recreates the topic_link records' do + TopicLink.expects(:extract_from).with(post) + post.recover! + end + end end end