From d5e4243f027fce7d1eac1824bb5c14c204cfe788 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 18 Mar 2013 15:12:31 -0400 Subject: [PATCH] Move draft sequence incrementing out of `after_save` --- app/models/post.rb | 9 +++++++-- lib/post_creator.rb | 3 +++ lib/post_revisor.rb | 1 + spec/models/draft_spec.rb | 7 +++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 4acccaaac..4a52b111e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -2,6 +2,7 @@ require_dependency 'jobs' require_dependency 'pretty_text' require_dependency 'rate_limiter' require_dependency 'post_revisor' +require_dependency 'enum' require 'archetype' require 'digest/sha1' @@ -369,9 +370,13 @@ class Post < ActiveRecord::Base Notification.delete_all topic_id: topic_id, post_number: post_number end - after_save do - DraftSequence.next! last_editor_id, topic.draft_key if topic # could be deleted + def advance_draft_sequence + return if topic.blank? # could be deleted + DraftSequence.next!(last_editor_id, topic.draft_key) + end + + after_save do quoted_post_numbers << reply_to_post_number if reply_to_post_number.present? # Create a reply relationship between quoted posts and this new post diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 78ec0848c..c00c7d519 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -117,6 +117,9 @@ class PostCreator created_at: post.created_at, user: BasicUserSerializer.new(post.user).as_json(root: false), post_number: post.post_number) + + # Advance the draft sequence + post.advance_draft_sequence end post diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 8ff983dc8..3db0a7afe 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -13,6 +13,7 @@ class PostRevisor revise_post update_category_description post_process_post + @post.advance_draft_sequence true end diff --git a/spec/models/draft_spec.rb b/spec/models/draft_spec.rb index 680a77910..43d05bb5a 100644 --- a/spec/models/draft_spec.rb +++ b/spec/models/draft_spec.rb @@ -61,9 +61,12 @@ describe Draft do end it 'nukes the post draft when a post is created' do - p = Fabricate(:post) + user = Fabricate(:user) + topic = Fabricate(:topic) + p = PostCreator.new(user, raw: Fabricate.build(:post).raw, topic_id: topic.id).create Draft.set(p.user, p.topic.draft_key, 0,'hello') - Fabricate(:post, topic: p.topic, user: p.user) + + PostCreator.new(user, raw: Fabricate.build(:post).raw).create Draft.get(p.user, p.topic.draft_key, DraftSequence.current(p.user, p.topic.draft_key)).should be_nil end