Merge pull request #888 from novemberkilo/master

Refactor to reduce complexity of Post#save_reply_relationships
This commit is contained in:
Robin Ward 2013-05-24 07:04:28 -07:00
commit c0c97487d8

View File

@ -435,21 +435,15 @@ class Post < ActiveRecord::Base
self.quote_count = temp_collector.size self.quote_count = temp_collector.size
end end
def save_reply_relationships def save_reply_relationships
self.quoted_post_numbers ||= [] add_to_quoted_post_numbers(reply_to_post_number)
self.quoted_post_numbers << reply_to_post_number if reply_to_post_number.present? return if self.quoted_post_numbers.blank?
# Create a reply relationship between quoted posts and this new post # Create a reply relationship between quoted posts and this new post
if self.quoted_post_numbers.present? self.quoted_post_numbers.each do |p|
self.quoted_post_numbers.map(&:to_i).uniq.each do |p|
post = Post.where(topic_id: topic_id, post_number: p).first post = Post.where(topic_id: topic_id, post_number: p).first
if post.present? create_reply_relationship_with(post)
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
end end
@ -488,6 +482,19 @@ class Post < ActiveRecord::Base
args args
end end
def add_to_quoted_post_numbers(num)
return unless num.present?
self.quoted_post_numbers ||= []
self.quoted_post_numbers << num
end
def create_reply_relationship_with(post)
return if post.nil?
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
# == Schema Information # == Schema Information