Skip to content

Commit

Permalink
Moved more Post callbacks into service classes
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed Mar 18, 2013
1 parent d5e4243 commit 897d48b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
38 changes: 19 additions & 19 deletions app/models/post.rb
Expand Up @@ -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)

Expand Down Expand Up @@ -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 = []

Expand All @@ -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 }
Expand Down
5 changes: 5 additions & 0 deletions lib/post_creator.rb
Expand Up @@ -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
Expand Down Expand Up @@ -120,6 +122,9 @@ def create

# Advance the draft sequence
post.advance_draft_sequence

# Save the quote relationships
post.save_reply_relationships
end

post
Expand Down
2 changes: 2 additions & 0 deletions lib/post_revisor.rb
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/components/jobs/exporter_spec.rb
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions spec/fabricators/post_fabricator.rb
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions spec/models/post_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 897d48b

Please sign in to comment.