FEATURE: search auto scopes on topic first

This commit is contained in:
Sam
2014-02-17 13:54:51 +11:00
parent 0438abf139
commit 2b10fdc97f
4 changed files with 72 additions and 12 deletions

View File

@ -1,6 +1,10 @@
class Search
class SearchResult
class TextHelper
extend ActionView::Helpers::TextHelper
end
attr_accessor :type, :id
# Category attributes
@ -36,14 +40,27 @@ class Search
end
end
def self.from_topic(t)
SearchResult.new(type: :topic, id: t.id, title: t.title, url: t.relative_url)
def self.from_topic(t, custom_title=nil)
SearchResult.new(type: :topic, id: t.id, title: custom_title || t.title, url: t.relative_url)
end
def self.from_post(p)
def self.from_post(p, context, term)
custom_title =
if context && context.id == p.topic_id
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",
post_number: p.post_number,
username: p.user && p.user.username,
excerpt: excerpt
)
end
if p.post_number == 1
# we want the topic link when it's the OP
SearchResult.from_topic(p.topic)
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)
else
SearchResult.new(type: :topic, id: p.topic.id, title: p.topic.title, url: p.url)
end