diff --git a/app/models/post.rb b/app/models/post.rb index 4a52b111e..012d092fc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -42,8 +42,6 @@ class Post < ActiveRecord::Base REGULAR = 1 MODERATOR_ACTION = 2 - before_save :extract_quoted_post_numbers - scope :by_newest, order('created_at desc, id desc') scope :with_user, includes(:user) @@ -376,23 +374,7 @@ class Post < ActiveRecord::Base 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 - if quoted_post_numbers.present? - quoted_post_numbers.map(&:to_i).uniq.each do |p| - post = Post.where(topic_id: topic_id, post_number: p).first - if post.present? - post_reply = post.post_replies.new(reply_id: id) - if post_reply.save - Post.update_all ['reply_count = reply_count + 1'], id: post.id - end - end - end - end - end - + # Determine what posts are quoted by this post def extract_quoted_post_numbers self.quoted_post_numbers = [] @@ -418,6 +400,24 @@ class Post < ActiveRecord::Base self.quote_count = quoted_post_numbers.size end + def save_reply_relationships + self.quoted_post_numbers ||= [] + self.quoted_post_numbers << reply_to_post_number if reply_to_post_number.present? + + # Create a reply relationship between quoted posts and this new post + if self.quoted_post_numbers.present? + self.quoted_post_numbers.map(&:to_i).uniq.each do |p| + post = Post.where(topic_id: topic_id, post_number: p).first + if post.present? + post_reply = post.post_replies.new(reply_id: id) + if post_reply.save + Post.update_all ['reply_count = reply_count + 1'], id: post.id + end + end + end + end + end + # Enqueue post processing for this post def trigger_post_process args = { post_id: id } diff --git a/lib/post_creator.rb b/lib/post_creator.rb index c00c7d519..ccc6407ff 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -77,6 +77,8 @@ class PostCreator post = topic.posts.new(raw: @opts[:raw], user: @user, reply_to_post_number: @opts[:reply_to_post_number]) + post.extract_quoted_post_numbers + post.image_sizes = @opts[:image_sizes] if @opts[:image_sizes].present? post.invalidate_oneboxes = @opts[:invalidate_oneboxes] if @opts[:invalidate_oneboxes].present? unless post.save @@ -120,6 +122,9 @@ class PostCreator # Advance the draft sequence post.advance_draft_sequence + + # Save the quote relationships + post.save_reply_relationships end post diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 3db0a7afe..862daaa3a 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -78,7 +78,9 @@ class PostRevisor PostAction.clear_flags!(@post, -1) end + @post.extract_quoted_post_numbers @post.save + @post.save_reply_relationships end def update_category_description diff --git a/spec/components/jobs/exporter_spec.rb b/spec/components/jobs/exporter_spec.rb index 3b245ac80..f35435892 100644 --- a/spec/components/jobs/exporter_spec.rb +++ b/spec/components/jobs/exporter_spec.rb @@ -91,8 +91,11 @@ describe Jobs::Exporter do @post1 = Fabricate(:post, topic: @topic1, user: @user1) @post1 = Fabricate(:post, topic: @topic3, user: @user1) @reply1 = Fabricate(:basic_reply, user: @user2, topic: @topic3) + @reply1.save_reply_relationships @reply2 = Fabricate(:basic_reply, user: @user1, topic: @topic1) + @reply2.save_reply_relationships @reply3 = Fabricate(:basic_reply, user: @user1, topic: @topic3) + @reply3.save_reply_relationships end it "should export all rows from the topics table in ascending id order" do diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index a3748136b..7636465bf 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -51,17 +51,6 @@ Fabricator(:reply, from: :post) do ' end -Fabricator(:multi_quote_reply, from: :post) do - user(:coding_horror) - topic - raw ' - [quote="Evil Trout, post:1"]post1 quote[/quote] - Aha! - [quote="Evil Trout, post:2"]post2 quote[/quote] - Neat-o - ' -end - Fabricator(:post_with_external_links, from: :post) do user topic diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 8bc2e7aac..43a9b2dc5 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -533,6 +533,7 @@ describe Post do describe 'with a reply' do let!(:reply) { Fabricate(:basic_reply, user: coding_horror, topic: post.topic) } + let!(:post_reply) { PostReply.create(post_id: post.id, reply_id: reply.id) } it 'changes the post count of the topic' do post.reload @@ -623,7 +624,7 @@ describe Post do end - describe 'quote counts' do + describe 'extract_quoted_post_numbers' do let!(:post) { Fabricate(:post, post_args) } let(:reply) { Fabricate.build(:post, post_args) } @@ -644,8 +645,11 @@ describe Post do describe 'a new reply' do - let!(:post) { Fabricate(:post, post_args) } - let!(:reply) { Fabricate(:reply, post_args.merge(reply_to_post_number: post.post_number)) } + let(:topic) { Fabricate(:topic) } + let(:other_user) { Fabricate(:coding_horror) } + let(:reply_text) { "[quote=\"Evil Trout, post:1\"]\nhello\n[/quote]\nHmmm!"} + let!(:post) { PostCreator.new(topic.user, raw: Fabricate.build(:post).raw, topic_id: topic.id).create } + let!(:reply) { PostCreator.new(other_user, raw: reply_text, topic_id: topic.id, reply_to_post_number: post.post_number ).create } it 'has a quote' do reply.quote_count.should == 1 @@ -683,7 +687,10 @@ describe Post do context 'a multi-quote reply' do - let!(:multi_reply) { Fabricate(:multi_quote_reply, post_args.merge(reply_to_post_number: post.post_number)) } + let!(:multi_reply) do + raw = "[quote=\"Evil Trout, post:1\"]post1 quote[/quote]\nAha!\n[quote=\"Evil Trout, post:2\"]post2 quote[/quote]\nNeat-o" + PostCreator.new(other_user, raw: raw, topic_id: topic.id, reply_to_post_number: post.post_number).create + end it 'has two quotes' do multi_reply.quote_count.should == 2