2013-05-23 14:26:51 -04:00
|
|
|
class Search
|
|
|
|
|
|
|
|
class SearchResult
|
2014-02-16 21:54:51 -05:00
|
|
|
class TextHelper
|
|
|
|
extend ActionView::Helpers::TextHelper
|
|
|
|
end
|
|
|
|
|
2014-04-11 16:07:39 -04:00
|
|
|
attr_accessor :type, :id, :topic_id, :blurb
|
2013-05-23 14:26:51 -04:00
|
|
|
|
|
|
|
# Category attributes
|
|
|
|
attr_accessor :color, :text_color
|
|
|
|
|
|
|
|
# User attributes
|
2014-05-29 00:38:52 -04:00
|
|
|
attr_accessor :avatar_template, :uploaded_avatar_id
|
2013-05-23 14:26:51 -04:00
|
|
|
|
|
|
|
def initialize(row)
|
|
|
|
row.symbolize_keys!
|
|
|
|
@type = row[:type].to_sym
|
2014-04-11 16:07:39 -04:00
|
|
|
@url, @id, @title, @topic_id, @blurb = row[:url], row[:id], row[:title], row[:topic_id], row[:blurb]
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2013-11-30 00:43:44 -05:00
|
|
|
def as_json(options = nil)
|
2013-05-23 14:26:51 -04:00
|
|
|
json = {id: @id, title: @title, url: @url}
|
2014-05-29 00:38:52 -04:00
|
|
|
[ :avatar_template,
|
|
|
|
:uploaded_avatar_id,
|
|
|
|
:color,
|
|
|
|
:text_color,
|
|
|
|
:blurb
|
|
|
|
].each do |k|
|
|
|
|
val = send(k)
|
|
|
|
json[k] = val if val
|
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
json
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_category(c)
|
2014-02-16 12:45:00 -05:00
|
|
|
SearchResult.new(type: :category, id: c.id, title: c.name, url: c.url).tap do |r|
|
2013-05-23 14:26:51 -04:00
|
|
|
r.color = c.color
|
|
|
|
r.text_color = c.text_color
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_user(u)
|
2014-05-29 00:38:52 -04:00
|
|
|
SearchResult.new(
|
|
|
|
type: :user,
|
|
|
|
id: u.username_lower,
|
|
|
|
title: u.username,
|
|
|
|
url: "/users/#{u.username_lower}").tap do |r|
|
|
|
|
r.uploaded_avatar_id = u.uploaded_avatar_id
|
|
|
|
r.avatar_template = u.avatar_template
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-11 16:07:39 -04:00
|
|
|
def self.from_topic(t, options = {})
|
|
|
|
SearchResult.new(type: :topic, topic_id: t.id, id: t.id, title: options[:custom_title] || t.title, url: t.relative_url, blurb: options[:custom_blurb])
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2014-04-11 16:07:39 -04:00
|
|
|
def self.from_post(p, context, term, include_blurbs=false)
|
2014-02-16 21:54:51 -05:00
|
|
|
custom_title =
|
|
|
|
if context && context.id == p.topic_id
|
2014-02-16 22:34:14 -05:00
|
|
|
# TODO: rewrite this
|
|
|
|
# 1. convert markdown to text
|
|
|
|
# 2. grab full words
|
2014-02-16 21:54:51 -05:00
|
|
|
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
|
2014-04-11 16:07:39 -04:00
|
|
|
if include_blurbs
|
|
|
|
#add a blurb from the post to the search results
|
|
|
|
custom_blurb = TextHelper.excerpt(p.raw, term.split(/\s+/)[0], radius: 100)
|
|
|
|
custom_blurb = TextHelper.truncate(p.raw, length: 200) if custom_blurb.blank?
|
|
|
|
end
|
2013-06-29 15:45:15 -04:00
|
|
|
if p.post_number == 1
|
|
|
|
# we want the topic link when it's the OP
|
2014-04-11 16:07:39 -04:00
|
|
|
SearchResult.from_topic(p.topic, {custom_title: custom_title, custom_blurb: custom_blurb})
|
2014-02-16 21:54:51 -05:00
|
|
|
elsif context && context.id == p.topic_id
|
2014-04-11 16:07:39 -04:00
|
|
|
SearchResult.new(type: :topic, topic_id: p.topic_id, id: "_#{p.id}", title: custom_title, url: p.url, blurb: custom_blurb)
|
2013-06-29 15:45:15 -04:00
|
|
|
else
|
2014-04-11 16:07:39 -04:00
|
|
|
SearchResult.new(type: :topic, topic_id: p.topic_id, id: p.topic.id, title: p.topic.title, url: p.url, blurb: custom_blurb)
|
2013-06-29 15:45:15 -04:00
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|