Skip to content

Commit

Permalink
Extract SQL generation method
Browse files Browse the repository at this point in the history
  • Loading branch information
blowmage committed Feb 7, 2013
1 parent 972b9d7 commit 1cd565e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions app/models/user_search.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class UserSearch

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 "
if topic_id
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
Expand All @@ -26,13 +36,7 @@ def self.search term, topic_id
end

sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"

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
sql
end

end

0 comments on commit 1cd565e

Please sign in to comment.