From ef93512de83df4a5c8f86562b06c1d5f52375e87 Mon Sep 17 00:00:00 2001 From: Navin Date: Wed, 22 May 2013 21:38:45 +0200 Subject: [PATCH 1/2] Extract quote parsing into a method --- app/models/post.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 9651025483..2e0b4cddc3 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -421,12 +421,6 @@ class Post < ActiveRecord::Base self.quoted_post_numbers = [] # Create relationships for the quotes - raw.scan(/\[quote=\"([^"]+)"\]/).each do |m| - if m.present? - args = {} - m.first.scan(/([a-z]+)\:(\d+)/).each do |arg| - args[arg[0].to_sym] = arg[1].to_i - end if args[:topic].present? # If the topic attribute is present, ensure it's the same topic @@ -436,6 +430,8 @@ class Post < ActiveRecord::Base end end + raw.scan(/\[quote=\"([^"]+)"\]/).each do |quote| + args = parse_quote_into_arguments(quote) end self.quoted_post_numbers.uniq! @@ -485,4 +481,12 @@ class Post < ActiveRecord::Base def add_error_if_count_exceeded(key_for_translation, current_count, max_count) errors.add(:base, I18n.t(key_for_translation, count: max_count)) if current_count > max_count end + def parse_quote_into_arguments(quote) + return {} unless quote.present? + args = {} + quote.first.scan(/([a-z]+)\:(\d+)/).each do |arg| + args[arg[0].to_sym] = arg[1].to_i + end + args + end end From 475421636905e8f3d9c2d4352f1a2b8527ab3794 Mon Sep 17 00:00:00 2001 From: Navin Date: Wed, 22 May 2013 21:45:31 +0200 Subject: [PATCH 2/2] Simplify branching --- app/models/post.rb | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 2e0b4cddc3..295ffd46ce 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -416,26 +416,21 @@ class Post < ActiveRecord::Base DraftSequence.next!(last_editor_id, topic.draft_key) end + # Determine what posts are quoted by this post def extract_quoted_post_numbers - self.quoted_post_numbers = [] + temp_collector = [] # Create relationships for the quotes - - if args[:topic].present? - # If the topic attribute is present, ensure it's the same topic - self.quoted_post_numbers << args[:post] if topic_id == args[:topic] - else - self.quoted_post_numbers << args[:post] - end - - end raw.scan(/\[quote=\"([^"]+)"\]/).each do |quote| args = parse_quote_into_arguments(quote) + # If the topic attribute is present, ensure it's the same topic + temp_collector << args[:post] unless (args[:topic].present? && topic_id != args[:topic]) end - self.quoted_post_numbers.uniq! - self.quote_count = quoted_post_numbers.size + temp_collector.uniq! + self.quoted_post_numbers = temp_collector + self.quote_count = temp_collector.size end def save_reply_relationships @@ -481,6 +476,7 @@ class Post < ActiveRecord::Base def add_error_if_count_exceeded(key_for_translation, current_count, max_count) errors.add(:base, I18n.t(key_for_translation, count: max_count)) if current_count > max_count end + def parse_quote_into_arguments(quote) return {} unless quote.present? args = {} @@ -489,4 +485,5 @@ class Post < ActiveRecord::Base end args end + end