BUGFIX: search could break when expanding

This commit is contained in:
Sam 2014-02-17 14:34:14 +11:00
parent 2b10fdc97f
commit abb2de22ab
3 changed files with 13 additions and 7 deletions

View File

@ -12,7 +12,7 @@ class Search
def topic_ids
topic_results = @by_type[:topic]
return Set.new if topic_results.blank?
return topic_results.result_ids
return topic_results.results.map{|r| r.topic_id}
end
def as_json(options = nil)

View File

@ -5,7 +5,7 @@ class Search
extend ActionView::Helpers::TextHelper
end
attr_accessor :type, :id
attr_accessor :type, :id, :topic_id
# Category attributes
attr_accessor :color, :text_color
@ -16,7 +16,7 @@ class Search
def initialize(row)
row.symbolize_keys!
@type = row[:type].to_sym
@url, @id, @title = row[:url], row[:id], row[:title]
@url, @id, @title, @topic_id = row[:url], row[:id], row[:title], row[:topic_id]
end
def as_json(options = nil)
@ -41,12 +41,15 @@ class Search
end
def self.from_topic(t, custom_title=nil)
SearchResult.new(type: :topic, id: t.id, title: custom_title || t.title, url: t.relative_url)
SearchResult.new(type: :topic, topic_id: t.id, id: t.id, title: custom_title || t.title, url: t.relative_url)
end
def self.from_post(p, context, term)
custom_title =
if context && context.id == p.topic_id
# TODO: rewrite this
# 1. convert markdown to text
# 2. grab full words
excerpt = TextHelper.excerpt(p.raw, term.split(/\s+/)[0], radius: 30)
excerpt = TextHelper.truncate(p.raw, length: 50) if excerpt.blank?
I18n.t("search.within_post",
@ -60,9 +63,9 @@ class Search
# we want the topic link when it's the OP
SearchResult.from_topic(p.topic, custom_title)
elsif context && context.id == p.topic_id
SearchResult.new(type: :topic, id: "_#{p.id}", title: custom_title, url: p.url)
SearchResult.new(type: :topic, topic_id: p.topic_id, id: "_#{p.id}", title: custom_title, url: p.url)
else
SearchResult.new(type: :topic, id: p.topic.id, title: p.topic.title, url: p.url)
SearchResult.new(type: :topic, topic_id: p.topic_id, id: p.topic.id, title: p.topic.title, url: p.url)
end
end

View File

@ -157,6 +157,9 @@ describe Search do
"_#{post4.id}",
topic2.id]
# trigger expanded search
results = Search.new('birds', search_context: post1.topic).execute
end
end