mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 10:58:31 -05:00
Remove topic links when a post is deleted
This commit is contained in:
parent
49c09898e2
commit
4db8204a15
5 changed files with 57 additions and 13 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -56,6 +56,8 @@ describe Post do
|
|||
end
|
||||
|
||||
describe "versions and deleting/recovery" do
|
||||
|
||||
context 'a post without links' do
|
||||
let(:post) { Fabricate(:post, post_args) }
|
||||
|
||||
before do
|
||||
|
@ -77,6 +79,22 @@ describe Post 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
|
||||
|
||||
|
|
Loading…
Reference in a new issue