Merge pull request #881 from novemberkilo/master

Improve flog metric for Post#extract_quoted_post_numbers
This commit is contained in:
Sam 2013-05-23 00:21:27 -07:00
commit 3dfc034e8d
1 changed files with 19 additions and 18 deletions

View File

@ -418,30 +418,21 @@ class Post < ActiveRecord::Base
DraftSequence.next!(last_editor_id, topic.draft_key) DraftSequence.next!(last_editor_id, topic.draft_key)
end end
# Determine what posts are quoted by this post # Determine what posts are quoted by this post
def extract_quoted_post_numbers def extract_quoted_post_numbers
self.quoted_post_numbers = [] temp_collector = []
# Create relationships for the quotes # Create relationships for the quotes
raw.scan(/\[quote=\"([^"]+)"\]/).each do |m| raw.scan(/\[quote=\"([^"]+)"\]/).each do |quote|
if m.present? args = parse_quote_into_arguments(quote)
args = {} # If the topic attribute is present, ensure it's the same topic
m.first.scan(/([a-z]+)\:(\d+)/).each do |arg| temp_collector << args[:post] unless (args[:topic].present? && topic_id != args[:topic])
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
self.quoted_post_numbers << args[:post] if topic_id == args[:topic]
else
self.quoted_post_numbers << args[:post]
end
end
end end
self.quoted_post_numbers.uniq! temp_collector.uniq!
self.quote_count = quoted_post_numbers.size self.quoted_post_numbers = temp_collector
self.quote_count = temp_collector.size
end end
def save_reply_relationships def save_reply_relationships
@ -487,4 +478,14 @@ class Post < ActiveRecord::Base
def add_error_if_count_exceeded(key_for_translation, current_count, max_count) 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 errors.add(:base, I18n.t(key_for_translation, count: max_count)) if current_count > max_count
end 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 end