Moved more Post callbacks into service classes

This commit is contained in:
Robin Ward 2013-03-18 15:54:08 -04:00
parent d5e4243f02
commit 897d48b145
6 changed files with 40 additions and 34 deletions

View file

@ -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 }

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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