FEATURE: when all posts in a topic are moved, close the topic

This commit is contained in:
Neil Lalonde 2015-12-29 16:01:49 -05:00
parent fd6bbc69e2
commit 98eee2b5de
2 changed files with 36 additions and 6 deletions

View file

@ -41,12 +41,18 @@ class PostMover
Guardian.new(user).ensure_can_see! topic
@destination_topic = topic
moving_all_posts = (@original_topic.posts.pluck(:id).sort == @post_ids.sort)
move_each_post
notify_users_that_posts_have_moved
update_statistics
update_user_actions
set_last_post_user_id(destination_topic)
if moving_all_posts
@original_topic.update_status('closed', true, @user)
end
destination_topic.reload
destination_topic
end

View file

@ -52,15 +52,16 @@ describe PostMover do
context "successfully moved" do
before do
topic.expects(:add_moderator_post)
TopicUser.update_last_read(user, topic.id, p4.post_number, 0)
TopicLink.extract_from(p2)
end
context "to a new topic" do
let!(:new_topic) { topic.move_posts(user, [p2.id, p4.id], title: "new testing topic name", category_id: category.id) }
it "works correctly" do
topic.expects(:add_moderator_post).once
new_topic = topic.move_posts(user, [p2.id, p4.id], title: "new testing topic name", category_id: category.id)
expect(TopicUser.find_by(user_id: user.id, topic_id: topic.id).last_read_post_number).to eq(p3.post_number)
expect(new_topic).to be_present
@ -97,15 +98,24 @@ describe PostMover do
action = UserAction.find_by(user_id: another_user.id)
expect(action.target_topic_id).to eq(new_topic.id)
end
it "moving all posts will close the topic" do
topic.expects(:add_moderator_post).twice
new_topic = topic.move_posts(user, [p1.id, p2.id, p3.id, p4.id], title: "new testing topic name", category_id: category.id)
expect(new_topic).to be_present
topic.reload
expect(topic.closed).to eq(true)
end
end
context "to an existing topic" do
let!(:destination_topic) { Fabricate(:topic, user: user ) }
let!(:destination_op) { Fabricate(:post, topic: destination_topic, user: user) }
let!(:moved_to) { topic.move_posts(user, [p2.id, p4.id], destination_topic_id: destination_topic.id)}
it "works correctly" do
topic.expects(:add_moderator_post).once
moved_to = topic.move_posts(user, [p2.id, p4.id], destination_topic_id: destination_topic.id)
expect(moved_to).to eq(destination_topic)
# Check out new topic
@ -144,13 +154,23 @@ describe PostMover do
# Should update last reads
expect(TopicUser.find_by(user_id: user.id, topic_id: topic.id).last_read_post_number).to eq(p3.post_number)
end
it "moving all posts will close the topic" do
topic.expects(:add_moderator_post).twice
moved_to = topic.move_posts(user, [p1.id, p2.id, p3.id, p4.id], destination_topic_id: destination_topic.id)
expect(moved_to).to be_present
topic.reload
expect(topic.closed).to eq(true)
end
end
context "moving the first post" do
let!(:new_topic) { topic.move_posts(user, [p1.id, p2.id], title: "new testing topic name") }
it "copies the OP, doesn't delete it" do
topic.expects(:add_moderator_post).once
new_topic = topic.move_posts(user, [p1.id, p2.id], title: "new testing topic name")
expect(new_topic).to be_present
new_topic.posts.reload
expect(new_topic.posts.by_post_number.first.raw).to eq(p1.raw)
@ -187,6 +207,10 @@ describe PostMover do
context "to an existing topic with a deleted post" do
before do
topic.expects(:add_moderator_post)
end
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) }