Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved more `Post` callbacks into service classes

  • Loading branch information...
commit 897d48b1450580302d47cc5af4d24a02cb4e15ba 1 parent d5e4243
@eviltrout eviltrout authored
View
38 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 @@ def advance_draft_sequence
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 @@ def extract_quoted_post_numbers
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
5 lib/post_creator.rb
@@ -77,6 +77,8 @@ def create
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 @@ def create
# Advance the draft sequence
post.advance_draft_sequence
+
+ # Save the quote relationships
+ post.save_reply_relationships
end
post
View
2  lib/post_revisor.rb
@@ -78,7 +78,9 @@ def update_post
PostAction.clear_flags!(@post, -1)
end
+ @post.extract_quoted_post_numbers
@post.save
+ @post.save_reply_relationships
end
def update_category_description
View
3  spec/components/jobs/exporter_spec.rb
@@ -91,8 +91,11 @@
@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
11 spec/fabricators/post_fabricator.rb
@@ -51,17 +51,6 @@
'
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
15 spec/models/post_spec.rb
@@ -533,6 +533,7 @@
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 @@
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 '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 @@
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

0 comments on commit 897d48b

Please sign in to comment.
Something went wrong with that request. Please try again.