mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 06:41:25 +08:00
FEATURE: search auto scopes on topic first
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user