mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
Extract SQL generation method
This commit is contained in:
parent
972b9d735c
commit
1cd565ec0d
1 changed files with 11 additions and 7 deletions
|
@ -1,6 +1,16 @@
|
||||||
class UserSearch
|
class UserSearch
|
||||||
|
|
||||||
def self.search term, topic_id
|
def self.search term, topic_id
|
||||||
|
sql = sql term, topic_id
|
||||||
|
results = User.exec_sql(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
|
||||||
|
results = results.map do |r|
|
||||||
|
r["avatar_template"] = User.avatar_template(r["email"])
|
||||||
|
r.delete("email")
|
||||||
|
r
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.sql term, topic_id
|
||||||
sql = "select username, name, email from users u "
|
sql = "select username, name, email from users u "
|
||||||
if topic_id
|
if topic_id
|
||||||
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
|
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
|
||||||
|
@ -26,13 +36,7 @@ class UserSearch
|
||||||
end
|
end
|
||||||
|
|
||||||
sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
|
sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
|
||||||
|
sql
|
||||||
results = User.exec_sql(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
|
|
||||||
results = results.map do |r|
|
|
||||||
r["avatar_template"] = User.avatar_template(r["email"])
|
|
||||||
r.delete("email")
|
|
||||||
r
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
Loading…
Reference in a new issue