From d7219a6fbc5ab38e92c7d6358f6f47f8937f9480 Mon Sep 17 00:00:00 2001 From: Navin Date: Thu, 23 May 2013 18:07:45 +0200 Subject: [PATCH 1/3] Helper to add a number to quoted_post_numbers --- app/models/post.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 1e690638156..199f81908a8 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -488,4 +488,9 @@ class Post < ActiveRecord::Base args end + def add_to_quoted_post_numbers(num) + return unless num.present? + self.quoted_post_numbers ||= [] + self.quoted_post_numbers << num + end end From ba4eaa02973ff4a685a78233499b41e45dbd5560 Mon Sep 17 00:00:00 2001 From: Navin Date: Thu, 23 May 2013 18:08:24 +0200 Subject: [PATCH 2/3] Helper to create reply relationship with post --- app/models/post.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 199f81908a8..1e5d1f7e6c2 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -493,4 +493,12 @@ class Post < ActiveRecord::Base 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 From 9aacde2f2d1dd503a16e9ba365844de4633e1b12 Mon Sep 17 00:00:00 2001 From: Navin Date: Thu, 23 May 2013 18:09:06 +0200 Subject: [PATCH 3/3] Refactor Post#save_reply_relationships --- app/models/post.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 1e5d1f7e6c2..58e23c0fe43 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -435,21 +435,15 @@ class Post < ActiveRecord::Base self.quote_count = temp_collector.size end + def save_reply_relationships - self.quoted_post_numbers ||= [] - self.quoted_post_numbers << reply_to_post_number if reply_to_post_number.present? + add_to_quoted_post_numbers(reply_to_post_number) + return if self.quoted_post_numbers.blank? # 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 + self.quoted_post_numbers.each do |p| + post = Post.where(topic_id: topic_id, post_number: p).first + create_reply_relationship_with(post) end end