From 96eecf18ef9fe6fec871092cdda62134bb98c94c Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 20 Aug 2014 12:28:34 -0400 Subject: [PATCH] FIX: moving a post to a topic with a deleted post should use correct post_number. Was getting unique index violation on (topic_id, post_number). --- app/models/topic.rb | 2 +- spec/models/post_mover_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/models/topic.rb b/app/models/topic.rb index e0fb6736b..ddb0b60fa 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -575,7 +575,7 @@ class Topic < ActiveRecord::Base end def max_post_number - posts.maximum(:post_number).to_i + posts.with_deleted.maximum(:post_number).to_i end def move_posts(moved_by, post_ids, opts) diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index b4e08d09c..c09177de2 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -173,6 +173,36 @@ describe PostMover do end + context "to an existing topic with a deleted post" do + + let!(:destination_topic) { Fabricate(:topic, user: user ) } + let!(:destination_op) { Fabricate(:post, topic: destination_topic, user: user) } + let!(:destination_deleted_reply) { Fabricate(:post, topic: destination_topic, user: another_user) } + let(:moved_to) { topic.move_posts(user, [p2.id, p4.id], destination_topic_id: destination_topic.id)} + + it "works correctly" do + destination_deleted_reply.trash! + + moved_to.should == destination_topic + + # Check out new topic + moved_to.reload + moved_to.posts_count.should == 3 + moved_to.highest_post_number.should == 4 + + # Posts should be re-ordered + p2.reload + p2.sort_order.should == 3 + p2.post_number.should == 3 + p2.topic_id.should == moved_to.id + + p4.reload + p4.post_number.should == 4 + p4.sort_order.should == 4 + p4.topic_id.should == moved_to.id + end + end + end end