2013-02-07 04:34:49 -05:00
|
|
|
class UserSearch
|
2013-02-07 06:50:59 -05:00
|
|
|
def self.search term, topic_id = nil
|
2013-02-10 07:37:24 -05:00
|
|
|
sql = User.sql_builder(
|
2013-08-14 09:25:05 -04:00
|
|
|
"select id, username, name, email, use_uploaded_avatar, uploaded_avatar_template, uploaded_avatar_id from users u
|
2013-02-10 07:37:24 -05:00
|
|
|
/*left_join*/
|
|
|
|
/*where*/
|
|
|
|
/*order_by*/")
|
2013-02-07 05:54:55 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-07 04:34:49 -05:00
|
|
|
if topic_id
|
2013-02-10 07:37:24 -05:00
|
|
|
sql.left_join "(select distinct p.user_id from posts p where topic_id = :topic_id) s on s.user_id = u.id", topic_id: topic_id
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
if term.present?
|
2013-02-10 07:47:04 -05:00
|
|
|
sql.where("username_lower like :term_like or
|
2013-02-07 04:34:49 -05:00
|
|
|
to_tsvector('simple', name) @@
|
|
|
|
to_tsquery('simple',
|
|
|
|
regexp_replace(
|
|
|
|
regexp_replace(
|
|
|
|
cast(plainto_tsquery(:term) as text)
|
|
|
|
,'\''(?: |$)', ':*''', 'g'),
|
|
|
|
'''', '', 'g')
|
2013-02-10 07:47:04 -05:00
|
|
|
)", term: term, term_like: "#{term.downcase}%")
|
2013-02-07 04:34:49 -05:00
|
|
|
|
2013-02-10 07:37:24 -05:00
|
|
|
sql.order_by "case when username_lower = :term then 0 else 1 end asc"
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-07 04:34:49 -05:00
|
|
|
if topic_id
|
2013-02-10 07:37:24 -05:00
|
|
|
sql.order_by "case when s.user_id is null then 0 else 1 end desc"
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
|
|
|
|
2013-02-10 07:37:24 -05:00
|
|
|
sql.order_by "case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
|
2013-02-07 05:54:55 -05:00
|
|
|
|
2013-02-10 07:37:24 -05:00
|
|
|
sql.exec
|
2013-02-07 04:34:49 -05:00
|
|
|
end
|
2013-02-10 07:37:24 -05:00
|
|
|
end
|