2013-05-23 14:26:51 -04:00
|
|
|
class Search
|
|
|
|
|
|
|
|
class SearchResult
|
|
|
|
attr_accessor :type, :id
|
|
|
|
|
|
|
|
# Category attributes
|
|
|
|
attr_accessor :color, :text_color
|
|
|
|
|
|
|
|
# User attributes
|
|
|
|
attr_accessor :avatar_template
|
|
|
|
|
|
|
|
def initialize(row)
|
|
|
|
row.symbolize_keys!
|
|
|
|
@type = row[:type].to_sym
|
2013-05-28 16:59:22 -04:00
|
|
|
@url, @id, @title = row[:url], row[:id], row[:title]
|
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}
|
|
|
|
json[:avatar_template] = @avatar_template if @avatar_template.present?
|
|
|
|
json[:color] = @color if @color.present?
|
|
|
|
json[:text_color] = @text_color if @text_color.present?
|
|
|
|
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)
|
|
|
|
SearchResult.new(type: :user, id: u.username_lower, title: u.username, url: "/users/#{u.username_lower}").tap do |r|
|
2013-08-15 18:26:22 -04:00
|
|
|
r.avatar_template = u.avatar_template
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_topic(t)
|
|
|
|
SearchResult.new(type: :topic, id: t.id, title: t.title, url: t.relative_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_post(p)
|
2013-06-29 15:45:15 -04:00
|
|
|
if p.post_number == 1
|
|
|
|
# we want the topic link when it's the OP
|
2013-08-15 18:26:22 -04:00
|
|
|
SearchResult.from_topic(p.topic)
|
2013-06-29 15:45:15 -04:00
|
|
|
else
|
|
|
|
SearchResult.new(type: :topic, id: p.topic.id, title: p.topic.title, url: p.url)
|
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|