Return User objects instead of hashes

This commit is contained in:
Mike Moore 2013-02-07 05:54:55 -05:00
parent 1cd565ec0d
commit 5b01ac9288
2 changed files with 11 additions and 9 deletions

View file

@ -294,7 +294,8 @@ class UsersController < ApplicationController
results = UserSearch.search term, topic_id
render :json => results
render json: { users: results.as_json( only: [ :username, :name ],
methods: :avatar_template ) }
end
private

View file

@ -1,15 +1,11 @@
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
User.find_by_sql sql(term, topic_id)
end
private
def self.sql term, topic_id
sql = "select username, name, email from users u "
if topic_id
@ -36,7 +32,12 @@ class UserSearch
end
sql << " case when last_seen_at is null then 0 else 1 end desc, last_seen_at desc, username asc limit(20)"
sql
sanitize_sql_array(sql, topic_id: topic_id, term_like: "#{term}%", term: term)
end
def self.sanitize_sql_array *args
ActiveRecord::Base.send(:sanitize_sql_array, args)
end
end