Remove topic links when a post is deleted

This commit is contained in:
Neil Lalonde 2013-06-13 13:41:45 -04:00
parent 49c09898e2
commit 4db8204a15
5 changed files with 57 additions and 13 deletions

View file

@ -25,6 +25,7 @@ class Post < ActiveRecord::Base
has_many :post_replies has_many :post_replies
has_many :replies, through: :post_replies has_many :replies, through: :post_replies
has_many :post_actions has_many :post_actions
has_many :topic_links
has_and_belongs_to_many :upload has_and_belongs_to_many :upload
@ -52,9 +53,15 @@ class Post < ActiveRecord::Base
@types ||= Enum.new(:regular, :moderator_action) @types ||= Enum.new(:regular, :moderator_action)
end end
def trash!
self.topic_links.each(&:destroy)
super
end
def recover! def recover!
super super
update_flagged_posts_count update_flagged_posts_count
TopicLink.extract_from(self)
end end
# The key we use in redis to ensure unique posts # The key we use in redis to ensure unique posts

View file

@ -13,7 +13,7 @@ class TopicLink < ActiveRecord::Base
validates_uniqueness_of :url, scope: [:topic_id, :post_id] 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 validate :link_to_self

View file

@ -69,6 +69,7 @@ class PostDestroyer
@post.revise(@user, I18n.t('js.post.deleted_by_author'), force_new_version: true) @post.revise(@user, I18n.t('js.post.deleted_by_author'), force_new_version: true)
@post.update_column(:user_deleted, true) @post.update_column(:user_deleted, true)
@post.update_flagged_posts_count @post.update_flagged_posts_count
@post.topic_links.each(&:destroy)
end end
end end

View file

@ -166,5 +166,23 @@ describe PostDestroyer do
end end
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 end

View file

@ -56,6 +56,8 @@ describe Post do
end end
describe "versions and deleting/recovery" do describe "versions and deleting/recovery" do
context 'a post without links' do
let(:post) { Fabricate(:post, post_args) } let(:post) { Fabricate(:post, post_args) }
before do before do
@ -77,6 +79,22 @@ describe Post do
post.versions.count.should == 0 post.versions.count.should == 0
end end
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 end